1 /* $NetBSD: mvsata.c,v 1.46 2018/11/12 20:54:03 jdolecek Exp $ */ 2 /* 3 * Copyright (c) 2008 KIYOHARA Takashi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: mvsata.c,v 1.46 2018/11/12 20:54:03 jdolecek Exp $"); 30 31 #include "opt_mvsata.h" 32 33 #include <sys/param.h> 34 #include <sys/buf.h> 35 #include <sys/bus.h> 36 #include <sys/cpu.h> 37 #include <sys/device.h> 38 #include <sys/disklabel.h> 39 #include <sys/errno.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/proc.h> 43 44 #include <machine/vmparam.h> 45 46 #include <dev/ata/atareg.h> 47 #include <dev/ata/atavar.h> 48 #include <dev/ic/wdcvar.h> 49 #include <dev/ata/satafisvar.h> 50 #include <dev/ata/satafisreg.h> 51 #include <dev/ata/satapmpreg.h> 52 #include <dev/ata/satareg.h> 53 #include <dev/ata/satavar.h> 54 55 #include <dev/scsipi/scsi_all.h> /* for SCSI status */ 56 57 #include "atapibus.h" 58 59 #include <dev/pci/pcidevs.h> /* XXX should not be here */ 60 61 /* 62 * Nice things to do: 63 * 64 * - MSI/MSI-X support - though on some models MSI actually doesn't work 65 * even when hardware claims to support it, according to FreeBSD/OpenBSD 66 * - move pci-specific code to the pci attach code 67 * - mvsata(4) use 64-bit DMA on hardware which claims to support it 68 * - e.g. AHA1430SA does not really work, crash in mvsata_intr() on boot 69 */ 70 71 #include <dev/ic/mvsatareg.h> 72 #include <dev/ic/mvsatavar.h> 73 74 #define MVSATA_DEV(sc) ((sc)->sc_wdcdev.sc_atac.atac_dev) 75 #define MVSATA_DEV2(mvport) ((mvport)->port_ata_channel.ch_atac->atac_dev) 76 77 #define MVSATA_HC_READ_4(hc, reg) \ 78 bus_space_read_4((hc)->hc_iot, (hc)->hc_ioh, (reg)) 79 #define MVSATA_HC_WRITE_4(hc, reg, val) \ 80 bus_space_write_4((hc)->hc_iot, (hc)->hc_ioh, (reg), (val)) 81 #define MVSATA_EDMA_READ_4(mvport, reg) \ 82 bus_space_read_4((mvport)->port_iot, (mvport)->port_ioh, (reg)) 83 #define MVSATA_EDMA_WRITE_4(mvport, reg, val) \ 84 bus_space_write_4((mvport)->port_iot, (mvport)->port_ioh, (reg), (val)) 85 #define MVSATA_WDC_READ_2(mvport, reg) \ 86 bus_space_read_2((mvport)->port_iot, (mvport)->port_ioh, \ 87 SHADOW_REG_BLOCK_OFFSET + (reg)) 88 #define MVSATA_WDC_READ_1(mvport, reg) \ 89 bus_space_read_1((mvport)->port_iot, (mvport)->port_ioh, \ 90 SHADOW_REG_BLOCK_OFFSET + (reg)) 91 #define MVSATA_WDC_WRITE_2(mvport, reg, val) \ 92 bus_space_write_2((mvport)->port_iot, (mvport)->port_ioh, \ 93 SHADOW_REG_BLOCK_OFFSET + (reg), (val)) 94 #define MVSATA_WDC_WRITE_1(mvport, reg, val) \ 95 bus_space_write_1((mvport)->port_iot, (mvport)->port_ioh, \ 96 SHADOW_REG_BLOCK_OFFSET + (reg), (val)) 97 98 #ifdef MVSATA_DEBUG 99 100 #define DEBUG_INTR 0x01 101 #define DEBUG_XFERS 0x02 102 #define DEBUG_FUNCS 0x08 103 #define DEBUG_PROBE 0x10 104 105 #define DPRINTF(n,x) if (mvsata_debug & (n)) printf x 106 int mvsata_debug = 0; 107 #else 108 #define DPRINTF(n,x) 109 #endif 110 111 #define ATA_DELAY 10000 /* 10s for a drive I/O */ 112 #define ATAPI_DELAY 10 /* 10 ms, this is used only before 113 sending a cmd */ 114 #define ATAPI_MODE_DELAY 1000 /* 1s, timeout for SET_FEATURE cmds */ 115 116 #define MVSATA_EPRD_MAX_SIZE (sizeof(struct eprd) * (MAXPHYS / PAGE_SIZE)) 117 118 119 static void mvsata_probe_drive(struct ata_channel *); 120 121 #ifndef MVSATA_WITHOUTDMA 122 static void mvsata_reset_channel(struct ata_channel *, int); 123 static int mvsata_bio(struct ata_drive_datas *, struct ata_xfer *); 124 static void mvsata_reset_drive(struct ata_drive_datas *, int, uint32_t *); 125 static int mvsata_exec_command(struct ata_drive_datas *, struct ata_xfer *); 126 static int mvsata_addref(struct ata_drive_datas *); 127 static void mvsata_delref(struct ata_drive_datas *); 128 static void mvsata_killpending(struct ata_drive_datas *); 129 130 #if NATAPIBUS > 0 131 static void mvsata_atapibus_attach(struct atabus_softc *); 132 static void mvsata_atapi_scsipi_request(struct scsipi_channel *, 133 scsipi_adapter_req_t, void *); 134 static void mvsata_atapi_minphys(struct buf *); 135 static void mvsata_atapi_probe_device(struct atapibus_softc *, int); 136 static void mvsata_atapi_kill_pending(struct scsipi_periph *); 137 #endif 138 #endif 139 140 static void mvsata_setup_channel(struct ata_channel *); 141 142 #ifndef MVSATA_WITHOUTDMA 143 static int mvsata_bio_start(struct ata_channel *, struct ata_xfer *); 144 static int mvsata_bio_intr(struct ata_channel *, struct ata_xfer *, int); 145 static void mvsata_bio_poll(struct ata_channel *, struct ata_xfer *); 146 static void mvsata_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int); 147 static void mvsata_bio_done(struct ata_channel *, struct ata_xfer *); 148 static int mvsata_bio_ready(struct mvsata_port *, struct ata_bio *, int, 149 int); 150 static int mvsata_wdc_cmd_start(struct ata_channel *, struct ata_xfer *); 151 static int mvsata_wdc_cmd_intr(struct ata_channel *, struct ata_xfer *, int); 152 static void mvsata_wdc_cmd_poll(struct ata_channel *, struct ata_xfer *); 153 static void mvsata_wdc_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, 154 int); 155 static void mvsata_wdc_cmd_done(struct ata_channel *, struct ata_xfer *); 156 static void mvsata_wdc_cmd_done_end(struct ata_channel *, struct ata_xfer *); 157 #if NATAPIBUS > 0 158 static int mvsata_atapi_start(struct ata_channel *, struct ata_xfer *); 159 static int mvsata_atapi_intr(struct ata_channel *, struct ata_xfer *, int); 160 static void mvsata_atapi_poll(struct ata_channel *, struct ata_xfer *); 161 static void mvsata_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, 162 int); 163 static void mvsata_atapi_reset(struct ata_channel *, struct ata_xfer *); 164 static void mvsata_atapi_phase_complete(struct ata_xfer *, int); 165 static void mvsata_atapi_done(struct ata_channel *, struct ata_xfer *); 166 static void mvsata_atapi_polldsc(void *); 167 #endif 168 169 static int mvsata_edma_enqueue(struct mvsata_port *, struct ata_xfer *); 170 static int mvsata_edma_handle(struct mvsata_port *, struct ata_xfer *); 171 static int mvsata_edma_wait(struct mvsata_port *, struct ata_xfer *, int); 172 static void mvsata_edma_rqq_remove(struct mvsata_port *, struct ata_xfer *); 173 #if NATAPIBUS > 0 174 static int mvsata_bdma_init(struct mvsata_port *, struct ata_xfer *); 175 static void mvsata_bdma_start(struct mvsata_port *); 176 #endif 177 #endif 178 179 static int mvsata_nondma_handle(struct mvsata_port *); 180 181 static int mvsata_port_init(struct mvsata_hc *, int); 182 static int mvsata_wdc_reg_init(struct mvsata_port *, struct wdc_regs *); 183 #ifndef MVSATA_WITHOUTDMA 184 static void mvsata_channel_recover(struct ata_channel *, int, uint32_t); 185 static void *mvsata_edma_resource_prepare(struct mvsata_port *, bus_dma_tag_t, 186 bus_dmamap_t *, size_t, int); 187 static void mvsata_edma_resource_purge(struct mvsata_port *, bus_dma_tag_t, 188 bus_dmamap_t, void *); 189 static int mvsata_dma_bufload(struct mvsata_port *, int, void *, size_t, int); 190 static inline void mvsata_dma_bufunload(struct mvsata_port *, int, int); 191 #endif 192 193 static void mvsata_hreset_port(struct mvsata_port *); 194 static void mvsata_reset_port(struct mvsata_port *); 195 static void mvsata_reset_hc(struct mvsata_hc *); 196 static uint32_t mvsata_softreset(struct mvsata_port *, int); 197 #ifndef MVSATA_WITHOUTDMA 198 static void mvsata_edma_reset_qptr(struct mvsata_port *); 199 static inline void mvsata_edma_enable(struct mvsata_port *); 200 static int mvsata_edma_disable(struct mvsata_port *, int, int); 201 static void mvsata_edma_config(struct mvsata_port *, enum mvsata_edmamode); 202 203 static void mvsata_edma_setup_crqb(struct mvsata_port *, int, 204 struct ata_xfer *); 205 #endif 206 static uint32_t mvsata_read_preamps_gen1(struct mvsata_port *); 207 static void mvsata_fix_phy_gen1(struct mvsata_port *); 208 static void mvsata_devconn_gen1(struct mvsata_port *); 209 210 static uint32_t mvsata_read_preamps_gen2(struct mvsata_port *); 211 static void mvsata_fix_phy_gen2(struct mvsata_port *); 212 #ifndef MVSATA_WITHOUTDMA 213 static void mvsata_edma_setup_crqb_gen2e(struct mvsata_port *, int, 214 struct ata_xfer *); 215 216 #ifdef MVSATA_DEBUG 217 static void mvsata_print_crqb(struct mvsata_port *, int); 218 static void mvsata_print_crpb(struct mvsata_port *, int); 219 static void mvsata_print_eprd(struct mvsata_port *, int); 220 #endif 221 222 static const struct ata_bustype mvsata_ata_bustype = { 223 SCSIPI_BUSTYPE_ATA, 224 mvsata_bio, 225 mvsata_reset_drive, 226 mvsata_reset_channel, 227 mvsata_exec_command, 228 ata_get_params, 229 mvsata_addref, 230 mvsata_delref, 231 mvsata_killpending, 232 mvsata_channel_recover, 233 }; 234 235 #if NATAPIBUS > 0 236 static const struct scsipi_bustype mvsata_atapi_bustype = { 237 SCSIPI_BUSTYPE_ATAPI, 238 atapi_scsipi_cmd, 239 atapi_interpret_sense, 240 atapi_print_addr, 241 mvsata_atapi_kill_pending, 242 NULL, 243 }; 244 #endif /* NATAPIBUS */ 245 #endif 246 247 static void 248 mvsata_pmp_select(struct mvsata_port *mvport, int pmpport) 249 { 250 uint32_t ifctl; 251 252 KASSERT(pmpport < PMP_MAX_DRIVES); 253 #if defined(DIAGNOSTIC) || defined(MVSATA_DEBUG) 254 if ((MVSATA_EDMA_READ_4(mvport, EDMA_CMD) & EDMA_CMD_EENEDMA) != 0) { 255 panic("EDMA enabled"); 256 } 257 #endif 258 259 ifctl = MVSATA_EDMA_READ_4(mvport, SATA_SATAICTL); 260 ifctl &= ~0xf; 261 ifctl |= pmpport; 262 MVSATA_EDMA_WRITE_4(mvport, SATA_SATAICTL, ifctl); 263 } 264 265 int 266 mvsata_attach(struct mvsata_softc *sc, const struct mvsata_product *product, 267 int (*mvsata_sreset)(struct mvsata_softc *), 268 int (*mvsata_misc_reset)(struct mvsata_softc *), 269 int read_pre_amps) 270 { 271 struct mvsata_hc *mvhc; 272 struct mvsata_port *mvport; 273 uint32_t (*read_preamps)(struct mvsata_port *) = NULL; 274 void (*_fix_phy)(struct mvsata_port *) = NULL; 275 #ifndef MVSATA_WITHOUTDMA 276 void (*edma_setup_crqb) 277 (struct mvsata_port *, int, struct ata_xfer *) = NULL; 278 #endif 279 int hc, port, channel; 280 281 aprint_normal_dev(MVSATA_DEV(sc), "Gen%s, %dhc, %dport/hc\n", 282 (product->generation == gen1) ? "I" : 283 ((product->generation == gen2) ? "II" : "IIe"), 284 product->hc, product->port); 285 286 287 switch (product->generation) { 288 case gen1: 289 mvsata_sreset = NULL; 290 read_pre_amps = 1; /* MUST */ 291 read_preamps = mvsata_read_preamps_gen1; 292 _fix_phy = mvsata_fix_phy_gen1; 293 #ifndef MVSATA_WITHOUTDMA 294 edma_setup_crqb = mvsata_edma_setup_crqb; 295 #endif 296 break; 297 298 case gen2: 299 read_preamps = mvsata_read_preamps_gen2; 300 _fix_phy = mvsata_fix_phy_gen2; 301 #ifndef MVSATA_WITHOUTDMA 302 edma_setup_crqb = mvsata_edma_setup_crqb; 303 #endif 304 break; 305 306 case gen2e: 307 read_preamps = mvsata_read_preamps_gen2; 308 _fix_phy = mvsata_fix_phy_gen2; 309 #ifndef MVSATA_WITHOUTDMA 310 edma_setup_crqb = mvsata_edma_setup_crqb_gen2e; 311 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NCQ; 312 #endif 313 break; 314 } 315 316 sc->sc_gen = product->generation; 317 sc->sc_hc = product->hc; 318 sc->sc_port = product->port; 319 sc->sc_flags = product->flags; 320 321 #ifdef MVSATA_WITHOUTDMA 322 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16; 323 #else 324 sc->sc_edma_setup_crqb = edma_setup_crqb; 325 sc->sc_wdcdev.sc_atac.atac_cap |= 326 (ATAC_CAP_DATA16 | ATAC_CAP_DMA | ATAC_CAP_UDMA); 327 #endif 328 sc->sc_wdcdev.sc_atac.atac_pio_cap = 4; 329 #ifdef MVSATA_WITHOUTDMA 330 sc->sc_wdcdev.sc_atac.atac_dma_cap = 0; 331 sc->sc_wdcdev.sc_atac.atac_udma_cap = 0; 332 #else 333 sc->sc_wdcdev.sc_atac.atac_dma_cap = 2; 334 sc->sc_wdcdev.sc_atac.atac_udma_cap = 6; 335 #endif 336 sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_ata_channels; 337 sc->sc_wdcdev.sc_atac.atac_nchannels = sc->sc_hc * sc->sc_port; 338 #ifndef MVSATA_WITHOUTDMA 339 sc->sc_wdcdev.sc_atac.atac_bustype_ata = &mvsata_ata_bustype; 340 #if NATAPIBUS > 0 341 sc->sc_wdcdev.sc_atac.atac_atapibus_attach = mvsata_atapibus_attach; 342 #endif 343 #endif 344 sc->sc_wdcdev.wdc_maxdrives = 1; /* SATA is always 1 drive */ 345 sc->sc_wdcdev.sc_atac.atac_probe = mvsata_probe_drive; 346 sc->sc_wdcdev.sc_atac.atac_set_modes = mvsata_setup_channel; 347 348 sc->sc_wdc_regs = 349 malloc(sizeof(struct wdc_regs) * product->hc * product->port, 350 M_DEVBUF, M_NOWAIT); 351 if (sc->sc_wdc_regs == NULL) { 352 aprint_error_dev(MVSATA_DEV(sc), 353 "can't allocate wdc regs memory\n"); 354 return ENOMEM; 355 } 356 sc->sc_wdcdev.regs = sc->sc_wdc_regs; 357 358 for (hc = 0; hc < sc->sc_hc; hc++) { 359 mvhc = &sc->sc_hcs[hc]; 360 mvhc->hc = hc; 361 mvhc->hc_sc = sc; 362 mvhc->hc_iot = sc->sc_iot; 363 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 364 hc * SATAHC_REGISTER_SIZE, SATAHC_REGISTER_SIZE, 365 &mvhc->hc_ioh)) { 366 aprint_error_dev(MVSATA_DEV(sc), 367 "can't subregion SATAHC %d registers\n", hc); 368 continue; 369 } 370 371 for (port = 0; port < sc->sc_port; port++) 372 if (mvsata_port_init(mvhc, port) == 0) { 373 int pre_amps; 374 375 mvport = mvhc->hc_ports[port]; 376 pre_amps = read_pre_amps ? 377 read_preamps(mvport) : 0x00000720; 378 mvport->_fix_phy_param.pre_amps = pre_amps; 379 mvport->_fix_phy_param._fix_phy = _fix_phy; 380 381 if (!mvsata_sreset) 382 mvsata_reset_port(mvport); 383 } 384 385 if (!mvsata_sreset) 386 mvsata_reset_hc(mvhc); 387 } 388 if (mvsata_sreset) 389 mvsata_sreset(sc); 390 391 if (mvsata_misc_reset) 392 mvsata_misc_reset(sc); 393 394 for (hc = 0; hc < sc->sc_hc; hc++) 395 for (port = 0; port < sc->sc_port; port++) { 396 mvport = sc->sc_hcs[hc].hc_ports[port]; 397 if (mvport == NULL) 398 continue; 399 if (mvsata_sreset) 400 mvport->_fix_phy_param._fix_phy(mvport); 401 } 402 for (channel = 0; channel < sc->sc_hc * sc->sc_port; channel++) 403 wdcattach(sc->sc_ata_channels[channel]); 404 405 return 0; 406 } 407 408 int 409 mvsata_intr(struct mvsata_hc *mvhc) 410 { 411 struct mvsata_softc *sc = mvhc->hc_sc; 412 struct mvsata_port *mvport; 413 uint32_t cause; 414 int port, handled = 0; 415 416 cause = MVSATA_HC_READ_4(mvhc, SATAHC_IC); 417 418 DPRINTF(DEBUG_INTR, ("%s:%d: mvsata_intr: cause=0x%08x\n", 419 device_xname(MVSATA_DEV(sc)), mvhc->hc, cause)); 420 421 if (cause & SATAHC_IC_SAINTCOAL) 422 MVSATA_HC_WRITE_4(mvhc, SATAHC_IC, ~SATAHC_IC_SAINTCOAL); 423 cause &= ~SATAHC_IC_SAINTCOAL; 424 425 for (port = 0; port < sc->sc_port; port++) { 426 mvport = mvhc->hc_ports[port]; 427 428 if (cause & SATAHC_IC_DONE(port)) { 429 #ifndef MVSATA_WITHOUTDMA 430 handled = mvsata_edma_handle(mvport, NULL); 431 #endif 432 MVSATA_HC_WRITE_4(mvhc, SATAHC_IC, 433 ~SATAHC_IC_DONE(port)); 434 } 435 436 if (cause & SATAHC_IC_SADEVINTERRUPT(port)) { 437 (void) mvsata_nondma_handle(mvport); 438 MVSATA_HC_WRITE_4(mvhc, SATAHC_IC, 439 ~SATAHC_IC_SADEVINTERRUPT(port)); 440 handled = 1; 441 } 442 } 443 444 return handled; 445 } 446 447 static int 448 mvsata_nondma_handle(struct mvsata_port *mvport) 449 { 450 struct ata_channel *chp = &mvport->port_ata_channel; 451 struct ata_xfer *xfer; 452 int ret; 453 454 /* 455 * The chip doesn't support several pending non-DMA commands, 456 * and the ata middle layer never issues several non-NCQ commands, 457 * so there must be exactly one active command at this moment. 458 */ 459 xfer = ata_queue_get_active_xfer(chp); 460 if (xfer == NULL) { 461 /* Can happen after error recovery, ignore */ 462 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 463 ("%s:%d: %s: intr without xfer\n", 464 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel, 465 __func__)); 466 return 0; 467 } 468 469 ret = xfer->ops->c_intr(chp, xfer, 1); 470 return (ret); 471 } 472 473 int 474 mvsata_error(struct mvsata_port *mvport) 475 { 476 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 477 uint32_t cause; 478 479 cause = MVSATA_EDMA_READ_4(mvport, EDMA_IEC); 480 /* 481 * We must ack SATA_SE and SATA_FISIC before acking coresponding bits 482 * in EDMA_IEC. 483 */ 484 if (cause & EDMA_IE_SERRINT) { 485 MVSATA_EDMA_WRITE_4(mvport, SATA_SE, 486 MVSATA_EDMA_READ_4(mvport, SATA_SEIM)); 487 } 488 if (cause & EDMA_IE_ETRANSINT) { 489 MVSATA_EDMA_WRITE_4(mvport, SATA_FISIC, 490 ~MVSATA_EDMA_READ_4(mvport, SATA_FISIM)); 491 } 492 MVSATA_EDMA_WRITE_4(mvport, EDMA_IEC, ~cause); 493 494 DPRINTF(DEBUG_INTR, ("%s:%d:%d:" 495 " mvsata_error: cause=0x%08x, mask=0x%08x, status=0x%08x\n", 496 device_xname(MVSATA_DEV2(mvport)), mvport->port_hc->hc, 497 mvport->port, cause, MVSATA_EDMA_READ_4(mvport, EDMA_IEM), 498 MVSATA_EDMA_READ_4(mvport, EDMA_S))); 499 500 cause &= MVSATA_EDMA_READ_4(mvport, EDMA_IEM); 501 if (!cause) 502 return 0; 503 504 if (cause & EDMA_IE_EDEVDIS) { 505 aprint_normal("%s:%d:%d: device disconnect\n", 506 device_xname(MVSATA_DEV2(mvport)), 507 mvport->port_hc->hc, mvport->port); 508 } 509 if (cause & EDMA_IE_EDEVCON) { 510 if (sc->sc_gen == gen1) 511 mvsata_devconn_gen1(mvport); 512 513 DPRINTF(DEBUG_INTR, (" device connected\n")); 514 } 515 516 #ifndef MVSATA_WITHOUTDMA 517 if ((sc->sc_gen == gen1 && cause & EDMA_IE_ETRANSINT) || 518 (sc->sc_gen != gen1 && cause & EDMA_IE_ESELFDIS)) { 519 switch (mvport->port_edmamode_curr) { 520 case dma: 521 case queued: 522 case ncq: 523 mvsata_edma_reset_qptr(mvport); 524 mvsata_edma_enable(mvport); 525 if (cause & EDMA_IE_EDEVERR) 526 break; 527 528 /* FALLTHROUGH */ 529 530 case nodma: 531 default: 532 aprint_error( 533 "%s:%d:%d: EDMA self disable happen 0x%x\n", 534 device_xname(MVSATA_DEV2(mvport)), 535 mvport->port_hc->hc, mvport->port, cause); 536 break; 537 } 538 } 539 #endif 540 if (cause & EDMA_IE_ETRANSINT) { 541 /* hot plug the Port Multiplier */ 542 aprint_normal("%s:%d:%d: detect Port Multiplier?\n", 543 device_xname(MVSATA_DEV2(mvport)), 544 mvport->port_hc->hc, mvport->port); 545 } 546 if (cause & EDMA_IE_EDEVERR) { 547 struct ata_channel *chp = &mvport->port_ata_channel; 548 549 aprint_error("%s:%d:%d: device error, recovering\n", 550 device_xname(MVSATA_DEV2(mvport)), 551 mvport->port_hc->hc, mvport->port); 552 553 ata_channel_lock(chp); 554 ata_thread_run(chp, 0, ATACH_TH_RECOVERY, 555 ATACH_ERR_ST(0, WDCS_ERR)); 556 ata_channel_unlock(chp); 557 } 558 559 return 1; 560 } 561 562 #ifndef MVSATA_WITHOUTDMA 563 static void 564 mvsata_channel_recover(struct ata_channel *chp, int flags, uint32_t tfd) 565 { 566 struct mvsata_port * const mvport = (struct mvsata_port *)chp; 567 int drive; 568 569 ata_channel_lock_owned(chp); 570 571 if (chp->ch_ndrives > PMP_PORT_CTL) { 572 /* Get PM port number for the device in error. This device 573 * doesn't seem to have dedicated register for this, so just 574 * assume last selected port was the one. */ 575 /* XXX FIS-based switching */ 576 drive = MVSATA_EDMA_READ_4(mvport, SATA_SATAICTL) & 0xf; 577 } else 578 drive = 0; 579 580 /* 581 * Controller doesn't need any special action. Simply execute 582 * READ LOG EXT for NCQ to unblock device processing, then continue 583 * as if nothing happened. 584 */ 585 586 ata_recovery_resume(chp, drive, tfd, AT_POLL); 587 588 /* Drive unblocked, back to normal operation */ 589 return; 590 } 591 #endif /* !MVSATA_WITHOUTDMA */ 592 593 /* 594 * ATA callback entry points 595 */ 596 597 static void 598 mvsata_probe_drive(struct ata_channel *chp) 599 { 600 struct mvsata_port * const mvport = (struct mvsata_port *)chp; 601 uint32_t sstat, sig; 602 603 ata_channel_lock(chp); 604 605 sstat = sata_reset_interface(chp, mvport->port_iot, 606 mvport->port_sata_scontrol, mvport->port_sata_sstatus, AT_WAIT); 607 switch (sstat) { 608 case SStatus_DET_DEV: 609 mvsata_pmp_select(mvport, PMP_PORT_CTL); 610 sig = mvsata_softreset(mvport, AT_WAIT); 611 sata_interpret_sig(chp, 0, sig); 612 break; 613 default: 614 break; 615 } 616 617 ata_channel_unlock(chp); 618 } 619 620 #ifndef MVSATA_WITHOUTDMA 621 static void 622 mvsata_reset_drive(struct ata_drive_datas *drvp, int flags, uint32_t *sigp) 623 { 624 struct ata_channel *chp = drvp->chnl_softc; 625 struct mvsata_port *mvport = (struct mvsata_port *)chp; 626 uint32_t edma_c; 627 uint32_t sig; 628 629 ata_channel_lock_owned(chp); 630 631 edma_c = MVSATA_EDMA_READ_4(mvport, EDMA_CMD); 632 633 DPRINTF(DEBUG_FUNCS, 634 ("%s:%d: mvsata_reset_drive: drive=%d (EDMA %sactive)\n", 635 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel, drvp->drive, 636 (edma_c & EDMA_CMD_EENEDMA) ? "" : "not ")); 637 638 if (edma_c & EDMA_CMD_EENEDMA) 639 mvsata_edma_disable(mvport, 10000, flags); 640 641 mvsata_pmp_select(mvport, drvp->drive); 642 643 sig = mvsata_softreset(mvport, flags); 644 645 if (sigp) 646 *sigp = sig; 647 648 if (edma_c & EDMA_CMD_EENEDMA) { 649 mvsata_edma_reset_qptr(mvport); 650 mvsata_edma_enable(mvport); 651 } 652 } 653 654 static void 655 mvsata_reset_channel(struct ata_channel *chp, int flags) 656 { 657 struct mvsata_port *mvport = (struct mvsata_port *)chp; 658 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 659 uint32_t sstat, ctrl; 660 661 DPRINTF(DEBUG_FUNCS, ("%s: mvsata_reset_channel: channel=%d\n", 662 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel)); 663 664 ata_channel_lock_owned(chp); 665 666 mvsata_hreset_port(mvport); 667 sstat = sata_reset_interface(chp, mvport->port_iot, 668 mvport->port_sata_scontrol, mvport->port_sata_sstatus, flags); 669 670 if (flags & AT_WAIT && sstat == SStatus_DET_DEV_NE && 671 sc->sc_gen != gen1) { 672 /* Downgrade to GenI */ 673 const uint32_t val = SControl_IPM_NONE | SControl_SPD_ANY | 674 SControl_DET_DISABLE; 675 676 MVSATA_EDMA_WRITE_4(mvport, mvport->port_sata_scontrol, val); 677 678 ctrl = MVSATA_EDMA_READ_4(mvport, SATA_SATAICFG); 679 ctrl &= ~(1 << 17); /* Disable GenII */ 680 MVSATA_EDMA_WRITE_4(mvport, SATA_SATAICFG, ctrl); 681 682 mvsata_hreset_port(mvport); 683 sata_reset_interface(chp, mvport->port_iot, 684 mvport->port_sata_scontrol, mvport->port_sata_sstatus, 685 flags); 686 } 687 688 ata_kill_active(chp, KILL_RESET, flags); 689 690 mvsata_edma_config(mvport, mvport->port_edmamode_curr); 691 mvsata_edma_reset_qptr(mvport); 692 mvsata_edma_enable(mvport); 693 } 694 695 static int 696 mvsata_addref(struct ata_drive_datas *drvp) 697 { 698 699 return 0; 700 } 701 702 static void 703 mvsata_delref(struct ata_drive_datas *drvp) 704 { 705 706 return; 707 } 708 709 static void 710 mvsata_killpending(struct ata_drive_datas *drvp) 711 { 712 713 return; 714 } 715 716 #if NATAPIBUS > 0 717 static void 718 mvsata_atapibus_attach(struct atabus_softc *ata_sc) 719 { 720 struct ata_channel *chp = ata_sc->sc_chan; 721 struct atac_softc *atac = chp->ch_atac; 722 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 723 struct scsipi_channel *chan = &chp->ch_atapi_channel; 724 725 /* 726 * Fill in the scsipi_adapter. 727 */ 728 adapt->adapt_dev = atac->atac_dev; 729 adapt->adapt_nchannels = atac->atac_nchannels; 730 adapt->adapt_request = mvsata_atapi_scsipi_request; 731 adapt->adapt_minphys = mvsata_atapi_minphys; 732 atac->atac_atapi_adapter.atapi_probe_device = mvsata_atapi_probe_device; 733 734 /* 735 * Fill in the scsipi_channel. 736 */ 737 memset(chan, 0, sizeof(*chan)); 738 chan->chan_adapter = adapt; 739 chan->chan_bustype = &mvsata_atapi_bustype; 740 chan->chan_channel = chp->ch_channel; 741 chan->chan_flags = SCSIPI_CHAN_OPENINGS; 742 chan->chan_openings = 1; 743 chan->chan_max_periph = 1; 744 chan->chan_ntargets = 1; 745 chan->chan_nluns = 1; 746 747 chp->atapibus = 748 config_found_ia(ata_sc->sc_dev, "atapi", chan, atapiprint); 749 } 750 751 static void 752 mvsata_atapi_minphys(struct buf *bp) 753 { 754 755 if (bp->b_bcount > MAXPHYS) 756 bp->b_bcount = MAXPHYS; 757 minphys(bp); 758 } 759 760 static void 761 mvsata_atapi_probe_device(struct atapibus_softc *sc, int target) 762 { 763 struct scsipi_channel *chan = sc->sc_channel; 764 struct scsipi_periph *periph; 765 struct ataparams ids; 766 struct ataparams *id = &ids; 767 struct mvsata_softc *mvc = 768 device_private(chan->chan_adapter->adapt_dev); 769 struct atac_softc *atac = &mvc->sc_wdcdev.sc_atac; 770 struct ata_channel *chp = atac->atac_channels[chan->chan_channel]; 771 struct ata_drive_datas *drvp = &chp->ch_drive[target]; 772 struct scsipibus_attach_args sa; 773 char serial_number[21], model[41], firmware_revision[9]; 774 int s; 775 776 /* skip if already attached */ 777 if (scsipi_lookup_periph(chan, target, 0) != NULL) 778 return; 779 780 /* if no ATAPI device detected at attach time, skip */ 781 if (drvp->drive_type != ATA_DRIVET_ATAPI) { 782 DPRINTF(DEBUG_PROBE, ("%s:%d: mvsata_atapi_probe_device:" 783 " drive %d not present\n", 784 device_xname(atac->atac_dev), chp->ch_channel, target)); 785 return; 786 } 787 788 /* Some ATAPI devices need a bit more time after software reset. */ 789 delay(5000); 790 if (ata_get_params(drvp, AT_WAIT, id) == 0) { 791 #ifdef ATAPI_DEBUG_PROBE 792 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 793 device_xname(sc->sc_dev), target, 794 id->atap_config & ATAPI_CFG_CMD_MASK, 795 id->atap_config & ATAPI_CFG_DRQ_MASK); 796 #endif 797 periph = scsipi_alloc_periph(M_NOWAIT); 798 if (periph == NULL) { 799 aprint_error_dev(atac->atac_dev, 800 "unable to allocate periph" 801 " for channel %d drive %d\n", 802 chp->ch_channel, target); 803 return; 804 } 805 periph->periph_dev = NULL; 806 periph->periph_channel = chan; 807 periph->periph_switch = &atapi_probe_periphsw; 808 periph->periph_target = target; 809 periph->periph_lun = 0; 810 periph->periph_quirks = PQUIRK_ONLYBIG; 811 812 #ifdef SCSIPI_DEBUG 813 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI && 814 SCSIPI_DEBUG_TARGET == target) 815 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS; 816 #endif 817 periph->periph_type = ATAPI_CFG_TYPE(id->atap_config); 818 if (id->atap_config & ATAPI_CFG_REMOV) 819 periph->periph_flags |= PERIPH_REMOVABLE; 820 if (periph->periph_type == T_SEQUENTIAL) { 821 s = splbio(); 822 drvp->drive_flags |= ATA_DRIVE_ATAPIDSCW; 823 splx(s); 824 } 825 826 sa.sa_periph = periph; 827 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 828 sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ? 829 T_REMOV : T_FIXED; 830 strnvisx(model, sizeof(model), id->atap_model, 40, 831 VIS_TRIM|VIS_SAFE|VIS_OCTAL); 832 strnvisx(serial_number, sizeof(serial_number), id->atap_serial, 833 20, VIS_TRIM|VIS_SAFE|VIS_OCTAL); 834 strnvisx(firmware_revision, sizeof(firmware_revision), 835 id->atap_revision, 8, VIS_TRIM|VIS_SAFE|VIS_OCTAL); 836 sa.sa_inqbuf.vendor = model; 837 sa.sa_inqbuf.product = serial_number; 838 sa.sa_inqbuf.revision = firmware_revision; 839 840 /* 841 * Determine the operating mode capabilities of the device. 842 */ 843 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 844 periph->periph_cap |= PERIPH_CAP_CMD16; 845 /* XXX This is gross. */ 846 periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK); 847 848 drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa); 849 850 if (drvp->drv_softc) 851 ata_probe_caps(drvp); 852 else { 853 s = splbio(); 854 drvp->drive_type = ATA_DRIVET_NONE; 855 splx(s); 856 } 857 } else { 858 DPRINTF(DEBUG_PROBE, ("%s:%d: mvsata_atapi_probe_device:" 859 " ATAPI_IDENTIFY_DEVICE failed for drive %d: error\n", 860 device_xname(atac->atac_dev), chp->ch_channel, target)); 861 s = splbio(); 862 drvp->drive_type = ATA_DRIVET_NONE; 863 splx(s); 864 } 865 } 866 867 /* 868 * Kill off all pending xfers for a periph. 869 * 870 * Must be called at splbio(). 871 */ 872 static void 873 mvsata_atapi_kill_pending(struct scsipi_periph *periph) 874 { 875 struct atac_softc *atac = 876 device_private(periph->periph_channel->chan_adapter->adapt_dev); 877 struct ata_channel *chp = 878 atac->atac_channels[periph->periph_channel->chan_channel]; 879 880 ata_kill_pending(&chp->ch_drive[periph->periph_target]); 881 } 882 #endif /* NATAPIBUS > 0 */ 883 #endif /* MVSATA_WITHOUTDMA */ 884 885 886 /* 887 * mvsata_setup_channel() 888 * Setup EDMA registers and prepare/purge DMA resources. 889 * We assuming already stopped the EDMA. 890 */ 891 static void 892 mvsata_setup_channel(struct ata_channel *chp) 893 { 894 #ifndef MVSATA_WITHOUTDMA 895 struct mvsata_port *mvport = (struct mvsata_port *)chp; 896 struct ata_drive_datas *drvp; 897 int drive, s; 898 uint32_t edma_mode = nodma; 899 int i; 900 const int crqb_size = sizeof(union mvsata_crqb) * MVSATA_EDMAQ_LEN; 901 const int crpb_size = sizeof(struct crpb) * MVSATA_EDMAQ_LEN; 902 const int eprd_buf_size = MVSATA_EPRD_MAX_SIZE * MVSATA_EDMAQ_LEN; 903 904 DPRINTF(DEBUG_FUNCS, ("%s:%d: mvsata_setup_channel: ", 905 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel)); 906 907 for (drive = 0; drive < chp->ch_ndrives; drive++) { 908 drvp = &chp->ch_drive[drive]; 909 910 /* If no drive, skip */ 911 if (drvp->drive_type == ATA_DRIVET_NONE) 912 continue; 913 914 if (drvp->drive_flags & ATA_DRIVE_UDMA) { 915 /* use Ultra/DMA */ 916 s = splbio(); 917 drvp->drive_flags &= ~ATA_DRIVE_DMA; 918 splx(s); 919 } 920 921 if (drvp->drive_flags & (ATA_DRIVE_UDMA | ATA_DRIVE_DMA)) { 922 if (drvp->drive_flags & ATA_DRIVE_NCQ) 923 edma_mode = ncq; 924 else if (drvp->drive_type == ATA_DRIVET_ATA) 925 edma_mode = dma; 926 } 927 } 928 929 DPRINTF(DEBUG_FUNCS, 930 ("EDMA %sactive mode\n", (edma_mode == nodma) ? "not " : "")); 931 932 if (edma_mode == nodma) { 933 no_edma: 934 if (mvport->port_crqb != NULL) 935 mvsata_edma_resource_purge(mvport, mvport->port_dmat, 936 mvport->port_crqb_dmamap, mvport->port_crqb); 937 if (mvport->port_crpb != NULL) 938 mvsata_edma_resource_purge(mvport, mvport->port_dmat, 939 mvport->port_crpb_dmamap, mvport->port_crpb); 940 if (mvport->port_eprd != NULL) 941 mvsata_edma_resource_purge(mvport, mvport->port_dmat, 942 mvport->port_eprd_dmamap, mvport->port_eprd); 943 944 return; 945 } 946 947 if (mvport->port_crqb == NULL) 948 mvport->port_crqb = mvsata_edma_resource_prepare(mvport, 949 mvport->port_dmat, &mvport->port_crqb_dmamap, crqb_size, 1); 950 if (mvport->port_crpb == NULL) 951 mvport->port_crpb = mvsata_edma_resource_prepare(mvport, 952 mvport->port_dmat, &mvport->port_crpb_dmamap, crpb_size, 0); 953 if (mvport->port_eprd == NULL) { 954 mvport->port_eprd = mvsata_edma_resource_prepare(mvport, 955 mvport->port_dmat, &mvport->port_eprd_dmamap, eprd_buf_size, 956 1); 957 for (i = 0; i < MVSATA_EDMAQ_LEN; i++) { 958 mvport->port_reqtbl[i].eprd_offset = 959 i * MVSATA_EPRD_MAX_SIZE; 960 mvport->port_reqtbl[i].eprd = mvport->port_eprd + 961 i * MVSATA_EPRD_MAX_SIZE / sizeof(struct eprd); 962 } 963 } 964 965 if (mvport->port_crqb == NULL || mvport->port_crpb == NULL || 966 mvport->port_eprd == NULL) { 967 aprint_error_dev(MVSATA_DEV2(mvport), 968 "channel %d: can't use EDMA\n", chp->ch_channel); 969 s = splbio(); 970 for (drive = 0; drive < chp->ch_ndrives; drive++) { 971 drvp = &chp->ch_drive[drive]; 972 973 /* If no drive, skip */ 974 if (drvp->drive_type == ATA_DRIVET_NONE) 975 continue; 976 977 drvp->drive_flags &= ~(ATA_DRIVE_UDMA | ATA_DRIVE_DMA); 978 } 979 splx(s); 980 goto no_edma; 981 } 982 983 mvsata_edma_config(mvport, edma_mode); 984 mvsata_edma_reset_qptr(mvport); 985 mvsata_edma_enable(mvport); 986 #endif 987 } 988 989 #ifndef MVSATA_WITHOUTDMA 990 static const struct ata_xfer_ops mvsata_bio_xfer_ops = { 991 .c_start = mvsata_bio_start, 992 .c_intr = mvsata_bio_intr, 993 .c_poll = mvsata_bio_poll, 994 .c_abort = mvsata_bio_done, 995 .c_kill_xfer = mvsata_bio_kill_xfer, 996 }; 997 998 static int 999 mvsata_bio(struct ata_drive_datas *drvp, struct ata_xfer *xfer) 1000 { 1001 struct ata_channel *chp = drvp->chnl_softc; 1002 struct atac_softc *atac = chp->ch_atac; 1003 struct ata_bio *ata_bio = &xfer->c_bio; 1004 1005 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1006 ("%s:%d: mvsata_bio: drive=%d, blkno=%" PRId64 1007 ", bcount=%ld\n", device_xname(atac->atac_dev), chp->ch_channel, 1008 drvp->drive, ata_bio->blkno, ata_bio->bcount)); 1009 1010 if (atac->atac_cap & ATAC_CAP_NOIRQ) 1011 ata_bio->flags |= ATA_POLL; 1012 if (ata_bio->flags & ATA_POLL) 1013 xfer->c_flags |= C_POLL; 1014 if ((drvp->drive_flags & (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) && 1015 (ata_bio->flags & ATA_SINGLE) == 0) 1016 xfer->c_flags |= C_DMA; 1017 xfer->c_drive = drvp->drive; 1018 xfer->c_databuf = ata_bio->databuf; 1019 xfer->c_bcount = ata_bio->bcount; 1020 xfer->ops = &mvsata_bio_xfer_ops; 1021 ata_exec_xfer(chp, xfer); 1022 return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED; 1023 } 1024 1025 static int 1026 mvsata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer) 1027 { 1028 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1029 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 1030 struct atac_softc *atac = chp->ch_atac; 1031 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1032 struct ata_bio *ata_bio = &xfer->c_bio; 1033 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 1034 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0; 1035 u_int16_t cyl; 1036 u_int8_t head, sect, cmd = 0; 1037 int nblks, error, tfd; 1038 1039 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, ("%s:%d: mvsata_bio_start: drive=%d\n", 1040 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive)); 1041 1042 ata_channel_lock_owned(chp); 1043 1044 if (xfer->c_flags & C_DMA) 1045 if (drvp->n_xfers <= NXFER) 1046 drvp->n_xfers++; 1047 1048 /* 1049 * 1050 * When starting a multi-sector transfer, or doing single-sector 1051 * transfers... 1052 */ 1053 if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) { 1054 if (ata_bio->flags & ATA_SINGLE) 1055 nblks = 1; 1056 else 1057 nblks = xfer->c_bcount / drvp->lp->d_secsize; 1058 /* Check for bad sectors and adjust transfer, if necessary. */ 1059 if ((drvp->lp->d_flags & D_BADSECT) != 0) { 1060 long blkdiff; 1061 int i; 1062 1063 for (i = 0; (blkdiff = drvp->badsect[i]) != -1; 1064 i++) { 1065 blkdiff -= ata_bio->blkno; 1066 if (blkdiff < 0) 1067 continue; 1068 if (blkdiff == 0) 1069 /* Replace current block of transfer. */ 1070 ata_bio->blkno = 1071 drvp->lp->d_secperunit - 1072 drvp->lp->d_nsectors - i - 1; 1073 if (blkdiff < nblks) { 1074 /* Bad block inside transfer. */ 1075 ata_bio->flags |= ATA_SINGLE; 1076 nblks = 1; 1077 } 1078 break; 1079 } 1080 /* Transfer is okay now. */ 1081 } 1082 if (xfer->c_flags & C_DMA) { 1083 enum mvsata_edmamode dmamode; 1084 1085 ata_bio->nblks = nblks; 1086 ata_bio->nbytes = xfer->c_bcount; 1087 1088 /* switch to appropriate dma mode if necessary */ 1089 dmamode = (xfer->c_flags & C_NCQ) ? ncq : dma; 1090 if (mvport->port_edmamode_curr != dmamode) 1091 mvsata_edma_config(mvport, dmamode); 1092 1093 if (xfer->c_flags & C_POLL) 1094 sc->sc_enable_intr(mvport, 0 /*off*/); 1095 error = mvsata_edma_enqueue(mvport, xfer); 1096 if (error) { 1097 if (error == EINVAL) { 1098 /* 1099 * We can't do DMA on this transfer 1100 * for some reason. Fall back to 1101 * PIO. 1102 */ 1103 xfer->c_flags &= ~C_DMA; 1104 error = 0; 1105 goto do_pio; 1106 } 1107 if (error == EBUSY) { 1108 aprint_error_dev(atac->atac_dev, 1109 "channel %d: EDMA Queue full\n", 1110 chp->ch_channel); 1111 /* 1112 * XXX: Perhaps, after it waits for 1113 * a while, it is necessary to call 1114 * bio_start again. 1115 */ 1116 } 1117 ata_bio->error = ERR_DMA; 1118 ata_bio->r_error = 0; 1119 return ATASTART_ABORT; 1120 } 1121 chp->ch_flags |= ATACH_DMA_WAIT; 1122 /* start timeout machinery */ 1123 if ((xfer->c_flags & C_POLL) == 0) 1124 callout_reset(&chp->c_timo_callout, 1125 mstohz(ATA_DELAY), ata_timeout, chp); 1126 /* wait for irq */ 1127 goto intr; 1128 } /* else not DMA */ 1129 do_pio: 1130 if (ata_bio->flags & ATA_LBA48) { 1131 sect = 0; 1132 cyl = 0; 1133 head = 0; 1134 } else if (ata_bio->flags & ATA_LBA) { 1135 sect = (ata_bio->blkno >> 0) & 0xff; 1136 cyl = (ata_bio->blkno >> 8) & 0xffff; 1137 head = (ata_bio->blkno >> 24) & 0x0f; 1138 head |= WDSD_LBA; 1139 } else { 1140 int blkno = ata_bio->blkno; 1141 sect = blkno % drvp->lp->d_nsectors; 1142 sect++; /* Sectors begin with 1, not 0. */ 1143 blkno /= drvp->lp->d_nsectors; 1144 head = blkno % drvp->lp->d_ntracks; 1145 blkno /= drvp->lp->d_ntracks; 1146 cyl = blkno; 1147 head |= WDSD_CHS; 1148 } 1149 ata_bio->nblks = uimin(nblks, drvp->multi); 1150 ata_bio->nbytes = ata_bio->nblks * drvp->lp->d_secsize; 1151 KASSERT(nblks == 1 || (ata_bio->flags & ATA_SINGLE) == 0); 1152 if (ata_bio->nblks > 1) 1153 cmd = (ata_bio->flags & ATA_READ) ? 1154 WDCC_READMULTI : WDCC_WRITEMULTI; 1155 else 1156 cmd = (ata_bio->flags & ATA_READ) ? 1157 WDCC_READ : WDCC_WRITE; 1158 1159 /* EDMA disable, if enabled this channel. */ 1160 KASSERT((chp->ch_flags & ATACH_NCQ) == 0); 1161 if (mvport->port_edmamode_curr != nodma) 1162 mvsata_edma_disable(mvport, 10 /* ms */, wait_flags); 1163 1164 mvsata_pmp_select(mvport, xfer->c_drive); 1165 1166 /* Do control operations specially. */ 1167 if (__predict_false(drvp->state < READY)) { 1168 /* 1169 * Actually, we want to be careful not to mess with 1170 * the control state if the device is currently busy, 1171 * but we can assume that we never get to this point 1172 * if that's the case. 1173 */ 1174 /* 1175 * If it's not a polled command, we need the kernel 1176 * thread 1177 */ 1178 if ((xfer->c_flags & C_POLL) == 0 && 1179 (chp->ch_flags & ATACH_TH_RUN) == 0) { 1180 return ATASTART_TH; 1181 } 1182 if (mvsata_bio_ready(mvport, ata_bio, xfer->c_drive, 1183 (xfer->c_flags & C_POLL) ? AT_POLL : 0) != 0) { 1184 return ATASTART_ABORT; 1185 } 1186 } 1187 1188 /* Initiate command! */ 1189 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 1190 switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags, &tfd)) { 1191 case WDCWAIT_OK: 1192 break; 1193 case WDCWAIT_TOUT: 1194 goto timeout; 1195 case WDCWAIT_THR: 1196 return ATASTART_TH; 1197 } 1198 if (ata_bio->flags & ATA_LBA48) 1199 wdccommandext(chp, 0, atacmd_to48(cmd), 1200 ata_bio->blkno, nblks, 0, WDSD_LBA); 1201 else 1202 wdccommand(chp, 0, cmd, cyl, 1203 head, sect, nblks, 1204 (drvp->lp->d_type == DKTYPE_ST506) ? 1205 drvp->lp->d_precompcyl / 4 : 0); 1206 1207 /* start timeout machinery */ 1208 if ((xfer->c_flags & C_POLL) == 0) 1209 callout_reset(&chp->c_timo_callout, 1210 mstohz(ATA_DELAY), wdctimeout, chp); 1211 } else if (ata_bio->nblks > 1) { 1212 /* The number of blocks in the last stretch may be smaller. */ 1213 nblks = xfer->c_bcount / drvp->lp->d_secsize; 1214 if (ata_bio->nblks > nblks) { 1215 ata_bio->nblks = nblks; 1216 ata_bio->nbytes = xfer->c_bcount; 1217 } 1218 } 1219 /* If this was a write and not using DMA, push the data. */ 1220 if ((ata_bio->flags & ATA_READ) == 0) { 1221 /* 1222 * we have to busy-wait here, we can't rely on running in 1223 * thread context. 1224 */ 1225 if (wdc_wait_for_drq(chp, ATA_DELAY, AT_POLL, &tfd) != 0) { 1226 aprint_error_dev(atac->atac_dev, 1227 "channel %d: drive %d timeout waiting for DRQ," 1228 " st=0x%02x, err=0x%02x\n", 1229 chp->ch_channel, xfer->c_drive, ATACH_ST(tfd), 1230 ATACH_ERR(tfd)); 1231 ata_bio->error = TIMEOUT; 1232 return ATASTART_ABORT; 1233 } 1234 if (ATACH_ST(tfd) & WDCS_ERR) { 1235 ata_bio->error = ERROR; 1236 ata_bio->r_error = ATACH_ERR(tfd); 1237 mvsata_bio_done(chp, xfer); 1238 return ATASTART_ABORT; 1239 } 1240 1241 wdc->dataout_pio(chp, drvp->drive_flags, 1242 (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes); 1243 } 1244 1245 intr: 1246 KASSERTMSG(((xfer->c_flags & C_DMA) != 0) 1247 == (mvport->port_edmamode_curr != nodma), 1248 "DMA mode mismatch: flags %x vs edmamode %d != %d", 1249 xfer->c_flags, mvport->port_edmamode_curr, nodma); 1250 1251 /* Wait for IRQ (either real or polled) */ 1252 if ((ata_bio->flags & ATA_POLL) != 0) 1253 return ATASTART_POLL; 1254 else 1255 return ATASTART_STARTED; 1256 1257 timeout: 1258 aprint_error_dev(atac->atac_dev, 1259 "channel %d: drive %d not ready, st=0x%02x, err=0x%02x\n", 1260 chp->ch_channel, xfer->c_drive, ATACH_ST(tfd), ATACH_ERR(tfd)); 1261 ata_bio->error = TIMEOUT; 1262 return ATASTART_ABORT; 1263 } 1264 1265 static void 1266 mvsata_bio_poll(struct ata_channel *chp, struct ata_xfer *xfer) 1267 { 1268 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1269 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 1270 1271 /* Wait for at last 400ns for status bit to be valid */ 1272 delay(1); 1273 if (chp->ch_flags & ATACH_DMA_WAIT) { 1274 mvsata_edma_wait(mvport, xfer, ATA_DELAY); 1275 sc->sc_enable_intr(mvport, 1 /*on*/); 1276 chp->ch_flags &= ~ATACH_DMA_WAIT; 1277 } 1278 1279 if ((xfer->c_bio.flags & ATA_ITSDONE) == 0) { 1280 KASSERT(xfer->c_flags & C_TIMEOU); 1281 mvsata_bio_intr(chp, xfer, 0); 1282 } 1283 } 1284 1285 static int 1286 mvsata_bio_intr(struct ata_channel *chp, struct ata_xfer *xfer, int intr_arg) 1287 { 1288 struct atac_softc *atac = chp->ch_atac; 1289 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1290 struct ata_bio *ata_bio = &xfer->c_bio; 1291 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 1292 int irq = ISSET(xfer->c_flags, (C_POLL|C_TIMEOU)) ? 0 : 1; 1293 int tfd = 0; 1294 1295 if (ISSET(xfer->c_flags, C_DMA|C_RECOVERED) && irq) { 1296 /* Invoked via mvsata_edma_handle() or recovery */ 1297 tfd = intr_arg; 1298 1299 if (tfd > 0 && ata_bio->error == NOERROR) { 1300 if (ATACH_ST(tfd) & WDCS_ERR) 1301 ata_bio->error = ERROR; 1302 if (ATACH_ST(tfd) & WDCS_BSY) 1303 ata_bio->error = TIMEOUT; 1304 ata_bio->r_error = ATACH_ERR(tfd); 1305 } 1306 } 1307 1308 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, ("%s:%d: %s: drive=%d\n", 1309 device_xname(atac->atac_dev), chp->ch_channel, __func__, 1310 xfer->c_drive)); 1311 1312 /* Cleanup EDMA if invoked from wdctimeout()/ata_timeout() */ 1313 if (ISSET(xfer->c_flags, C_TIMEOU) && ISSET(xfer->c_flags, C_DMA) 1314 && !ISSET(xfer->c_flags, C_POLL)) { 1315 mvsata_edma_rqq_remove((struct mvsata_port *)chp, xfer); 1316 } 1317 1318 ata_channel_lock(chp); 1319 1320 chp->ch_flags &= ~(ATACH_DMA_WAIT); 1321 1322 /* 1323 * If we missed an interrupt transfer, reset and restart. 1324 * Don't try to continue transfer, we may have missed cycles. 1325 */ 1326 if (xfer->c_flags & C_TIMEOU) { 1327 ata_bio->error = TIMEOUT; 1328 ata_channel_unlock(chp); 1329 mvsata_bio_done(chp, xfer); 1330 return 1; 1331 } 1332 1333 /* Is it not a transfer, but a control operation? */ 1334 if (!(xfer->c_flags & C_DMA) && drvp->state < READY) { 1335 aprint_error_dev(atac->atac_dev, 1336 "channel %d: drive %d bad state %d in %s\n", 1337 chp->ch_channel, xfer->c_drive, drvp->state, __func__); 1338 panic("%s: bad state", __func__); 1339 } 1340 1341 /* Ack interrupt done by wdc_wait_for_unbusy */ 1342 if (!(xfer->c_flags & C_DMA) && 1343 (wdc_wait_for_unbusy(chp, (irq == 0) ? ATA_DELAY : 0, AT_POLL, &tfd) 1344 == WDCWAIT_TOUT)) { 1345 if (irq && (xfer->c_flags & C_TIMEOU) == 0) { 1346 ata_channel_unlock(chp); 1347 return 0; /* IRQ was not for us */ 1348 } 1349 aprint_error_dev(atac->atac_dev, 1350 "channel %d: drive %d timeout, c_bcount=%d, c_skip%d\n", 1351 chp->ch_channel, xfer->c_drive, xfer->c_bcount, 1352 xfer->c_skip); 1353 ata_bio->error = TIMEOUT; 1354 ata_channel_unlock(chp); 1355 mvsata_bio_done(chp, xfer); 1356 return 1; 1357 } 1358 1359 if (xfer->c_flags & C_DMA) { 1360 if (ata_bio->error == NOERROR) 1361 goto end; 1362 if (ata_bio->error == ERR_DMA) { 1363 ata_dmaerr(drvp, 1364 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 1365 ata_channel_unlock(chp); 1366 goto err; 1367 } 1368 } 1369 1370 /* if we had an error, end */ 1371 if (ata_bio->error != NOERROR) { 1372 ata_channel_unlock(chp); 1373 err: 1374 mvsata_bio_done(chp, xfer); 1375 return 1; 1376 } 1377 1378 /* If this was a read and not using DMA, fetch the data. */ 1379 if ((ata_bio->flags & ATA_READ) != 0) { 1380 if ((ATACH_ST(tfd) & WDCS_DRQ) != WDCS_DRQ) { 1381 aprint_error_dev(atac->atac_dev, 1382 "channel %d: drive %d read intr before drq\n", 1383 chp->ch_channel, xfer->c_drive); 1384 ata_bio->error = TIMEOUT; 1385 ata_channel_unlock(chp); 1386 mvsata_bio_done(chp, xfer); 1387 return 1; 1388 } 1389 wdc->datain_pio(chp, drvp->drive_flags, 1390 (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes); 1391 } 1392 1393 end: 1394 ata_bio->blkno += ata_bio->nblks; 1395 ata_bio->blkdone += ata_bio->nblks; 1396 xfer->c_skip += ata_bio->nbytes; 1397 xfer->c_bcount -= ata_bio->nbytes; 1398 1399 /* See if this transfer is complete. */ 1400 if (xfer->c_bcount > 0) { 1401 if ((ata_bio->flags & ATA_POLL) == 0) { 1402 /* Start the next operation */ 1403 ata_xfer_start(xfer); 1404 } else { 1405 /* Let mvsata_bio_start do the loop */ 1406 } 1407 ata_channel_unlock(chp); 1408 } else { /* Done with this transfer */ 1409 ata_bio->error = NOERROR; 1410 ata_channel_unlock(chp); 1411 mvsata_bio_done(chp, xfer); 1412 } 1413 return 1; 1414 } 1415 1416 static void 1417 mvsata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason) 1418 { 1419 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1420 struct atac_softc *atac = chp->ch_atac; 1421 struct ata_bio *ata_bio = &xfer->c_bio; 1422 int drive = xfer->c_drive; 1423 bool deactivate = true; 1424 1425 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1426 ("%s:%d: mvsata_bio_kill_xfer: drive=%d\n", 1427 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive)); 1428 1429 /* EDMA restart, if enabled */ 1430 if (!(xfer->c_flags & C_DMA) && mvport->port_edmamode_curr != nodma) { 1431 mvsata_edma_reset_qptr(mvport); 1432 mvsata_edma_enable(mvport); 1433 } 1434 1435 ata_bio->flags |= ATA_ITSDONE; 1436 switch (reason) { 1437 case KILL_GONE_INACTIVE: 1438 deactivate = false; 1439 /* FALLTHROUGH */ 1440 case KILL_GONE: 1441 ata_bio->error = ERR_NODEV; 1442 break; 1443 case KILL_RESET: 1444 ata_bio->error = ERR_RESET; 1445 break; 1446 case KILL_REQUEUE: 1447 ata_bio->error = REQUEUE; 1448 break; 1449 default: 1450 aprint_error_dev(atac->atac_dev, 1451 "mvsata_bio_kill_xfer: unknown reason %d\n", reason); 1452 panic("mvsata_bio_kill_xfer"); 1453 } 1454 ata_bio->r_error = WDCE_ABRT; 1455 1456 if (deactivate) 1457 ata_deactivate_xfer(chp, xfer); 1458 1459 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer); 1460 } 1461 1462 static void 1463 mvsata_bio_done(struct ata_channel *chp, struct ata_xfer *xfer) 1464 { 1465 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1466 struct ata_bio *ata_bio = &xfer->c_bio; 1467 int drive = xfer->c_drive; 1468 bool iserror = (ata_bio->error != NOERROR); 1469 1470 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1471 ("%s:%d: mvsata_bio_done: drive=%d, flags=0x%x\n", 1472 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel, xfer->c_drive, 1473 (u_int)xfer->c_flags)); 1474 1475 /* EDMA restart, if enabled */ 1476 if (!(xfer->c_flags & C_DMA) && mvport->port_edmamode_curr != nodma) { 1477 mvsata_edma_reset_qptr(mvport); 1478 mvsata_edma_enable(mvport); 1479 } 1480 1481 if (ata_waitdrain_xfer_check(chp, xfer)) 1482 return; 1483 1484 /* feed back residual bcount to our caller */ 1485 ata_bio->bcount = xfer->c_bcount; 1486 1487 /* mark controller inactive and free xfer */ 1488 ata_deactivate_xfer(chp, xfer); 1489 1490 ata_bio->flags |= ATA_ITSDONE; 1491 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer); 1492 if (!iserror) 1493 atastart(chp); 1494 } 1495 1496 static int 1497 mvsata_bio_ready(struct mvsata_port *mvport, struct ata_bio *ata_bio, int drive, 1498 int flags) 1499 { 1500 struct ata_channel *chp = &mvport->port_ata_channel; 1501 struct atac_softc *atac = chp->ch_atac; 1502 struct ata_drive_datas *drvp = &chp->ch_drive[drive]; 1503 const char *errstring; 1504 int tfd; 1505 1506 flags |= AT_POLL; /* XXX */ 1507 1508 ata_channel_lock_owned(chp); 1509 1510 /* 1511 * disable interrupts, all commands here should be quick 1512 * enough to be able to poll, and we don't go here that often 1513 */ 1514 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT | WDCTL_IDS); 1515 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 1516 DELAY(10); 1517 errstring = "wait"; 1518 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, flags, &tfd)) 1519 goto ctrltimeout; 1520 wdccommandshort(chp, 0, WDCC_RECAL); 1521 /* Wait for at least 400ns for status bit to be valid */ 1522 DELAY(1); 1523 errstring = "recal"; 1524 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, flags, &tfd)) 1525 goto ctrltimeout; 1526 if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF)) 1527 goto ctrlerror; 1528 /* Don't try to set modes if controller can't be adjusted */ 1529 if (atac->atac_set_modes == NULL) 1530 goto geometry; 1531 /* Also don't try if the drive didn't report its mode */ 1532 if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0) 1533 goto geometry; 1534 wdccommand(chp, 0, SET_FEATURES, 0, 0, 0, 1535 0x08 | drvp->PIO_mode, WDSF_SET_MODE); 1536 errstring = "piomode-bio"; 1537 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, flags, &tfd)) 1538 goto ctrltimeout; 1539 if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF)) 1540 goto ctrlerror; 1541 if (drvp->drive_flags & ATA_DRIVE_UDMA) 1542 wdccommand(chp, 0, SET_FEATURES, 0, 0, 0, 1543 0x40 | drvp->UDMA_mode, WDSF_SET_MODE); 1544 else if (drvp->drive_flags & ATA_DRIVE_DMA) 1545 wdccommand(chp, 0, SET_FEATURES, 0, 0, 0, 1546 0x20 | drvp->DMA_mode, WDSF_SET_MODE); 1547 else 1548 goto geometry; 1549 errstring = "dmamode-bio"; 1550 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, flags, &tfd)) 1551 goto ctrltimeout; 1552 if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF)) 1553 goto ctrlerror; 1554 geometry: 1555 if (ata_bio->flags & ATA_LBA) 1556 goto multimode; 1557 wdccommand(chp, 0, WDCC_IDP, drvp->lp->d_ncylinders, 1558 drvp->lp->d_ntracks - 1, 0, drvp->lp->d_nsectors, 1559 (drvp->lp->d_type == DKTYPE_ST506) ? 1560 drvp->lp->d_precompcyl / 4 : 0); 1561 errstring = "geometry"; 1562 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, flags, &tfd)) 1563 goto ctrltimeout; 1564 if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF)) 1565 goto ctrlerror; 1566 multimode: 1567 if (drvp->multi == 1) 1568 goto ready; 1569 wdccommand(chp, 0, WDCC_SETMULTI, 0, 0, 0, drvp->multi, 0); 1570 errstring = "setmulti"; 1571 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, flags, &tfd)) 1572 goto ctrltimeout; 1573 if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF)) 1574 goto ctrlerror; 1575 ready: 1576 drvp->state = READY; 1577 /* 1578 * The drive is usable now 1579 */ 1580 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 1581 delay(10); /* some drives need a little delay here */ 1582 return 0; 1583 1584 ctrltimeout: 1585 aprint_error_dev(atac->atac_dev, "channel %d: drive %d %s timed out\n", 1586 chp->ch_channel, drive, errstring); 1587 ata_bio->error = TIMEOUT; 1588 goto ctrldone; 1589 ctrlerror: 1590 aprint_error_dev(atac->atac_dev, "channel %d: drive %d %s ", 1591 chp->ch_channel, drive, errstring); 1592 if (ATACH_ST(tfd) & WDCS_DWF) { 1593 aprint_error("drive fault\n"); 1594 ata_bio->error = ERR_DF; 1595 } else { 1596 ata_bio->r_error = ATACH_ERR(tfd); 1597 ata_bio->error = ERROR; 1598 aprint_error("error (%x)\n", ata_bio->r_error); 1599 } 1600 ctrldone: 1601 drvp->state = 0; 1602 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 1603 return -1; 1604 } 1605 1606 static const struct ata_xfer_ops mvsata_wdc_cmd_xfer_ops = { 1607 .c_start = mvsata_wdc_cmd_start, 1608 .c_intr = mvsata_wdc_cmd_intr, 1609 .c_poll = mvsata_wdc_cmd_poll, 1610 .c_abort = mvsata_wdc_cmd_done, 1611 .c_kill_xfer = mvsata_wdc_cmd_kill_xfer, 1612 }; 1613 1614 static int 1615 mvsata_exec_command(struct ata_drive_datas *drvp, struct ata_xfer *xfer) 1616 { 1617 struct ata_channel *chp = drvp->chnl_softc; 1618 struct ata_command *ata_c = &xfer->c_ata_c; 1619 int rv, s; 1620 1621 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1622 ("%s:%d: mvsata_exec_command: drive=%d, bcount=%d," 1623 " r_lba=0x%012"PRIx64", r_count=0x%04x, r_features=0x%04x," 1624 " r_device=0x%02x, r_command=0x%02x\n", 1625 device_xname(MVSATA_DEV2((struct mvsata_port *)chp)), 1626 chp->ch_channel, 1627 drvp->drive, ata_c->bcount, ata_c->r_lba, ata_c->r_count, 1628 ata_c->r_features, ata_c->r_device, ata_c->r_command)); 1629 1630 if (ata_c->flags & AT_POLL) 1631 xfer->c_flags |= C_POLL; 1632 if (ata_c->flags & AT_WAIT) 1633 xfer->c_flags |= C_WAIT; 1634 xfer->c_drive = drvp->drive; 1635 xfer->c_databuf = ata_c->data; 1636 xfer->c_bcount = ata_c->bcount; 1637 xfer->ops = &mvsata_wdc_cmd_xfer_ops; 1638 s = splbio(); 1639 ata_exec_xfer(chp, xfer); 1640 #ifdef DIAGNOSTIC 1641 if ((ata_c->flags & AT_POLL) != 0 && 1642 (ata_c->flags & AT_DONE) == 0) 1643 panic("mvsata_exec_command: polled command not done"); 1644 #endif 1645 if (ata_c->flags & AT_DONE) 1646 rv = ATACMD_COMPLETE; 1647 else { 1648 if (ata_c->flags & AT_WAIT) { 1649 ata_wait_cmd(chp, xfer); 1650 rv = ATACMD_COMPLETE; 1651 } else 1652 rv = ATACMD_QUEUED; 1653 } 1654 splx(s); 1655 return rv; 1656 } 1657 1658 static int 1659 mvsata_wdc_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer) 1660 { 1661 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1662 int drive = xfer->c_drive; 1663 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0; 1664 struct ata_command *ata_c = &xfer->c_ata_c; 1665 int tfd; 1666 1667 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1668 ("%s:%d: mvsata_cmd_start: drive=%d\n", 1669 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel, drive)); 1670 1671 ata_channel_lock_owned(chp); 1672 1673 /* First, EDMA disable, if enabled this channel. */ 1674 KASSERT((chp->ch_flags & ATACH_NCQ) == 0); 1675 if (mvport->port_edmamode_curr != nodma) 1676 mvsata_edma_disable(mvport, 10 /* ms */, wait_flags); 1677 1678 mvsata_pmp_select(mvport, drive); 1679 1680 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 1681 switch(wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ, 1682 ata_c->r_st_bmask, ata_c->timeout, wait_flags, &tfd)) { 1683 case WDCWAIT_OK: 1684 break; 1685 case WDCWAIT_TOUT: 1686 ata_c->flags |= AT_TIMEOU; 1687 return ATASTART_ABORT; 1688 case WDCWAIT_THR: 1689 return ATASTART_TH; 1690 } 1691 if (ata_c->flags & AT_POLL) 1692 /* polled command, disable interrupts */ 1693 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT | WDCTL_IDS); 1694 if ((ata_c->flags & AT_LBA48) != 0) { 1695 wdccommandext(chp, 0, ata_c->r_command, 1696 ata_c->r_lba, ata_c->r_count, ata_c->r_features, 1697 ata_c->r_device & ~0x10); 1698 } else { 1699 wdccommand(chp, 0, ata_c->r_command, 1700 (ata_c->r_lba >> 8) & 0xffff, 1701 (((ata_c->flags & AT_LBA) != 0) ? WDSD_LBA : 0) | 1702 ((ata_c->r_lba >> 24) & 0x0f), 1703 ata_c->r_lba & 0xff, 1704 ata_c->r_count & 0xff, 1705 ata_c->r_features & 0xff); 1706 } 1707 1708 if ((ata_c->flags & AT_POLL) == 0) { 1709 callout_reset(&chp->c_timo_callout, ata_c->timeout / 1000 * hz, 1710 wdctimeout, chp); 1711 return ATASTART_STARTED; 1712 } 1713 1714 return ATASTART_POLL; 1715 } 1716 1717 static void 1718 mvsata_wdc_cmd_poll(struct ata_channel *chp, struct ata_xfer *xfer) 1719 { 1720 /* 1721 * Polled command. Wait for drive ready or drq. Done in intr(). 1722 * Wait for at last 400ns for status bit to be valid. 1723 */ 1724 delay(10); /* 400ns delay */ 1725 mvsata_wdc_cmd_intr(chp, xfer, 0); 1726 } 1727 1728 static int 1729 mvsata_wdc_cmd_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq) 1730 { 1731 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1732 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1733 struct ata_command *ata_c = &xfer->c_ata_c; 1734 int bcount = ata_c->bcount; 1735 char *data = ata_c->data; 1736 int wflags; 1737 int drive_flags; 1738 int tfd; 1739 1740 ata_channel_lock(chp); 1741 1742 if (ata_c->r_command == WDCC_IDENTIFY || 1743 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) 1744 /* 1745 * The IDENTIFY data has been designed as an array of 1746 * u_int16_t, so we can byteswap it on the fly. 1747 * Historically it's what we have always done so keeping it 1748 * here ensure binary backward compatibility. 1749 */ 1750 drive_flags = ATA_DRIVE_NOSTREAM | 1751 chp->ch_drive[xfer->c_drive].drive_flags; 1752 else 1753 /* 1754 * Other data structure are opaque and should be transfered 1755 * as is. 1756 */ 1757 drive_flags = chp->ch_drive[xfer->c_drive].drive_flags; 1758 1759 if ((ata_c->flags & (AT_WAIT | AT_POLL)) == (AT_WAIT | AT_POLL)) 1760 /* both wait and poll, we can kpause here */ 1761 wflags = AT_WAIT | AT_POLL; 1762 else 1763 wflags = AT_POLL; 1764 1765 again: 1766 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, ("%s:%d: %s: drive=%d\n", 1767 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel, 1768 __func__, xfer->c_drive)); 1769 1770 /* 1771 * after a ATAPI_SOFT_RESET, the device will have released the bus. 1772 * Reselect again, it doesn't hurt for others commands, and the time 1773 * penalty for the extra register write is acceptable, 1774 * wdc_exec_command() isn't called often (mostly for autoconfig) 1775 */ 1776 if ((xfer->c_flags & C_ATAPI) != 0) { 1777 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 1778 } 1779 if ((ata_c->flags & AT_XFDONE) != 0) { 1780 /* 1781 * We have completed a data xfer. The drive should now be 1782 * in its initial state 1783 */ 1784 if (wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ, 1785 ata_c->r_st_bmask, (irq == 0) ? ata_c->timeout : 0, 1786 wflags, &tfd) == WDCWAIT_TOUT) { 1787 if (irq && (xfer->c_flags & C_TIMEOU) == 0) { 1788 ata_channel_unlock(chp); 1789 return 0; /* IRQ was not for us */ 1790 } 1791 ata_c->flags |= AT_TIMEOU; 1792 } 1793 goto out; 1794 } 1795 if (wdcwait(chp, ata_c->r_st_pmask, ata_c->r_st_pmask, 1796 (irq == 0) ? ata_c->timeout : 0, wflags, &tfd) == WDCWAIT_TOUT) { 1797 if (irq && (xfer->c_flags & C_TIMEOU) == 0) { 1798 ata_channel_unlock(chp); 1799 return 0; /* IRQ was not for us */ 1800 } 1801 ata_c->flags |= AT_TIMEOU; 1802 goto out; 1803 } 1804 delay(20); /* XXXXX: Delay more times. */ 1805 if (ata_c->flags & AT_READ) { 1806 if ((ATACH_ST(tfd) & WDCS_DRQ) == 0) { 1807 ata_c->flags |= AT_TIMEOU; 1808 goto out; 1809 } 1810 wdc->datain_pio(chp, drive_flags, data, bcount); 1811 /* at this point the drive should be in its initial state */ 1812 ata_c->flags |= AT_XFDONE; 1813 /* 1814 * XXX checking the status register again here cause some 1815 * hardware to timeout. 1816 */ 1817 } else if (ata_c->flags & AT_WRITE) { 1818 if ((ATACH_ST(tfd) & WDCS_DRQ) == 0) { 1819 ata_c->flags |= AT_TIMEOU; 1820 goto out; 1821 } 1822 wdc->dataout_pio(chp, drive_flags, data, bcount); 1823 ata_c->flags |= AT_XFDONE; 1824 if ((ata_c->flags & AT_POLL) == 0) { 1825 callout_reset(&chp->c_timo_callout, 1826 mstohz(ata_c->timeout), wdctimeout, chp); 1827 ata_channel_unlock(chp); 1828 return 1; 1829 } else 1830 goto again; 1831 } 1832 out: 1833 if (ATACH_ST(tfd) & WDCS_DWF) 1834 ata_c->flags |= AT_DF; 1835 if (ATACH_ST(tfd) & WDCS_ERR) { 1836 ata_c->flags |= AT_ERROR; 1837 ata_c->r_error = ATACH_ERR(tfd); 1838 } 1839 ata_channel_unlock(chp); 1840 mvsata_wdc_cmd_done(chp, xfer); 1841 1842 if ((ATACH_ST(tfd) & WDCS_ERR) == 0) 1843 atastart(chp); 1844 1845 return 1; 1846 } 1847 1848 static void 1849 mvsata_wdc_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, 1850 int reason) 1851 { 1852 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1853 struct ata_command *ata_c = &xfer->c_ata_c; 1854 bool deactivate = true; 1855 1856 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1857 ("%s:%d: mvsata_cmd_kill_xfer: drive=%d\n", 1858 device_xname(MVSATA_DEV2(mvport)), chp->ch_channel, xfer->c_drive)); 1859 1860 switch (reason) { 1861 case KILL_GONE_INACTIVE: 1862 deactivate = false; 1863 /* FALLTHROUGH */ 1864 case KILL_GONE: 1865 ata_c->flags |= AT_GONE; 1866 break; 1867 case KILL_RESET: 1868 ata_c->flags |= AT_RESET; 1869 break; 1870 case KILL_REQUEUE: 1871 panic("%s: not supposed to be requeued\n", __func__); 1872 break; 1873 default: 1874 aprint_error_dev(MVSATA_DEV2(mvport), 1875 "mvsata_cmd_kill_xfer: unknown reason %d\n", reason); 1876 panic("mvsata_cmd_kill_xfer"); 1877 } 1878 1879 mvsata_wdc_cmd_done_end(chp, xfer); 1880 1881 if (deactivate) 1882 ata_deactivate_xfer(chp, xfer); 1883 } 1884 1885 static void 1886 mvsata_wdc_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer) 1887 { 1888 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1889 struct atac_softc *atac = chp->ch_atac; 1890 struct ata_command *ata_c = &xfer->c_ata_c; 1891 1892 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 1893 ("%s:%d: mvsata_cmd_done: drive=%d, flags=0x%x\n", 1894 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 1895 ata_c->flags)); 1896 1897 if (ata_waitdrain_xfer_check(chp, xfer)) 1898 return; 1899 1900 if ((ata_c->flags & AT_READREG) != 0 && 1901 device_is_active(atac->atac_dev) && 1902 (ata_c->flags & (AT_ERROR | AT_DF)) == 0) { 1903 ata_c->r_status = MVSATA_WDC_READ_1(mvport, SRB_CS); 1904 ata_c->r_error = MVSATA_WDC_READ_1(mvport, SRB_FE); 1905 ata_c->r_count = MVSATA_WDC_READ_1(mvport, SRB_SC); 1906 ata_c->r_lba = 1907 (uint64_t)MVSATA_WDC_READ_1(mvport, SRB_LBAL) << 0; 1908 ata_c->r_lba |= 1909 (uint64_t)MVSATA_WDC_READ_1(mvport, SRB_LBAM) << 8; 1910 ata_c->r_lba |= 1911 (uint64_t)MVSATA_WDC_READ_1(mvport, SRB_LBAH) << 16; 1912 ata_c->r_device = MVSATA_WDC_READ_1(mvport, SRB_H); 1913 if ((ata_c->flags & AT_LBA48) != 0) { 1914 if ((ata_c->flags & AT_POLL) != 0) { 1915 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, 1916 WDCTL_HOB|WDCTL_4BIT|WDCTL_IDS); 1917 } else { 1918 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, 1919 WDCTL_HOB|WDCTL_4BIT); 1920 } 1921 ata_c->r_count |= 1922 MVSATA_WDC_READ_1(mvport, SRB_SC) << 8; 1923 ata_c->r_lba |= 1924 (uint64_t)MVSATA_WDC_READ_1(mvport, SRB_LBAL) << 24; 1925 ata_c->r_lba |= 1926 (uint64_t)MVSATA_WDC_READ_1(mvport, SRB_LBAM) << 32; 1927 ata_c->r_lba |= 1928 (uint64_t)MVSATA_WDC_READ_1(mvport, SRB_LBAH) << 40; 1929 if ((ata_c->flags & AT_POLL) != 0) { 1930 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, 1931 WDCTL_4BIT|WDCTL_IDS); 1932 } else { 1933 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, 1934 WDCTL_4BIT); 1935 } 1936 } else { 1937 ata_c->r_lba |= 1938 (uint64_t)(ata_c->r_device & 0x0f) << 24; 1939 } 1940 } 1941 1942 if (ata_c->flags & AT_POLL) { 1943 /* enable interrupts */ 1944 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 1945 delay(10); /* some drives need a little delay here */ 1946 } 1947 1948 mvsata_wdc_cmd_done_end(chp, xfer); 1949 1950 ata_deactivate_xfer(chp, xfer); 1951 } 1952 1953 static void 1954 mvsata_wdc_cmd_done_end(struct ata_channel *chp, struct ata_xfer *xfer) 1955 { 1956 struct mvsata_port *mvport = (struct mvsata_port *)chp; 1957 struct ata_command *ata_c = &xfer->c_ata_c; 1958 1959 /* EDMA restart, if enabled */ 1960 if (mvport->port_edmamode_curr != nodma) { 1961 mvsata_edma_reset_qptr(mvport); 1962 mvsata_edma_enable(mvport); 1963 } 1964 1965 ata_c->flags |= AT_DONE; 1966 } 1967 1968 #if NATAPIBUS > 0 1969 static const struct ata_xfer_ops mvsata_atapi_xfer_ops = { 1970 .c_start = mvsata_atapi_start, 1971 .c_intr = mvsata_atapi_intr, 1972 .c_poll = mvsata_atapi_poll, 1973 .c_abort = mvsata_atapi_reset, 1974 .c_kill_xfer = mvsata_atapi_kill_xfer, 1975 }; 1976 1977 static void 1978 mvsata_atapi_scsipi_request(struct scsipi_channel *chan, 1979 scsipi_adapter_req_t req, void *arg) 1980 { 1981 struct scsipi_adapter *adapt = chan->chan_adapter; 1982 struct scsipi_periph *periph; 1983 struct scsipi_xfer *sc_xfer; 1984 struct mvsata_softc *sc = device_private(adapt->adapt_dev); 1985 struct atac_softc *atac = &sc->sc_wdcdev.sc_atac; 1986 struct ata_channel *chp = atac->atac_channels[chan->chan_channel]; 1987 struct ata_xfer *xfer; 1988 int drive, s; 1989 1990 switch (req) { 1991 case ADAPTER_REQ_RUN_XFER: 1992 sc_xfer = arg; 1993 periph = sc_xfer->xs_periph; 1994 drive = periph->periph_target; 1995 1996 if (!device_is_active(atac->atac_dev)) { 1997 sc_xfer->error = XS_DRIVER_STUFFUP; 1998 scsipi_done(sc_xfer); 1999 return; 2000 } 2001 xfer = ata_get_xfer(chp, false); 2002 if (xfer == NULL) { 2003 sc_xfer->error = XS_RESOURCE_SHORTAGE; 2004 scsipi_done(sc_xfer); 2005 return; 2006 } 2007 2008 if (sc_xfer->xs_control & XS_CTL_POLL) 2009 xfer->c_flags |= C_POLL; 2010 xfer->c_drive = drive; 2011 xfer->c_flags |= C_ATAPI; 2012 xfer->c_databuf = sc_xfer->data; 2013 xfer->c_bcount = sc_xfer->datalen; 2014 xfer->ops = &mvsata_atapi_xfer_ops; 2015 xfer->c_scsipi = sc_xfer; 2016 xfer->c_atapi.c_dscpoll = 0; 2017 s = splbio(); 2018 ata_exec_xfer(chp, xfer); 2019 #ifdef DIAGNOSTIC 2020 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 && 2021 (sc_xfer->xs_status & XS_STS_DONE) == 0) 2022 panic("mvsata_atapi_scsipi_request:" 2023 " polled command not done"); 2024 #endif 2025 splx(s); 2026 return; 2027 2028 default: 2029 /* Not supported, nothing to do. */ 2030 ; 2031 } 2032 } 2033 2034 static int 2035 mvsata_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer) 2036 { 2037 struct mvsata_softc *sc = (struct mvsata_softc *)chp->ch_atac; 2038 struct mvsata_port *mvport = (struct mvsata_port *)chp; 2039 struct atac_softc *atac = &sc->sc_wdcdev.sc_atac; 2040 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2041 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 2042 const int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0; 2043 const char *errstring; 2044 int tfd; 2045 2046 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 2047 ("%s:%d:%d: mvsata_atapi_start: scsi flags 0x%x\n", 2048 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 2049 xfer->c_drive, sc_xfer->xs_control)); 2050 2051 ata_channel_lock_owned(chp); 2052 2053 KASSERT((chp->ch_flags & ATACH_NCQ) == 0); 2054 if (mvport->port_edmamode_curr != nodma) 2055 mvsata_edma_disable(mvport, 10 /* ms */, wait_flags); 2056 2057 mvsata_pmp_select(mvport, xfer->c_drive); 2058 2059 if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER)) 2060 drvp->n_xfers++; 2061 2062 /* Do control operations specially. */ 2063 if (__predict_false(drvp->state < READY)) { 2064 /* If it's not a polled command, we need the kernel thread */ 2065 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0 && 2066 (chp->ch_flags & ATACH_TH_RUN) == 0) { 2067 return ATASTART_TH; 2068 } 2069 /* 2070 * disable interrupts, all commands here should be quick 2071 * enough to be able to poll, and we don't go here that often 2072 */ 2073 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT | WDCTL_IDS); 2074 2075 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 2076 /* Don't try to set mode if controller can't be adjusted */ 2077 if (atac->atac_set_modes == NULL) 2078 goto ready; 2079 /* Also don't try if the drive didn't report its mode */ 2080 if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0) 2081 goto ready; 2082 errstring = "unbusy"; 2083 if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd)) 2084 goto timeout; 2085 wdccommand(chp, 0, SET_FEATURES, 0, 0, 0, 2086 0x08 | drvp->PIO_mode, WDSF_SET_MODE); 2087 errstring = "piomode-atapi"; 2088 if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags, 2089 &tfd)) 2090 goto timeout; 2091 if (ATACH_ST(tfd) & WDCS_ERR) { 2092 if (ATACH_ERR(tfd) == WDCE_ABRT) { 2093 /* 2094 * Some ATAPI drives reject PIO settings. 2095 * Fall back to PIO mode 3 since that's the 2096 * minimum for ATAPI. 2097 */ 2098 aprint_error_dev(atac->atac_dev, 2099 "channel %d drive %d: PIO mode %d rejected," 2100 " falling back to PIO mode 3\n", 2101 chp->ch_channel, xfer->c_drive, 2102 drvp->PIO_mode); 2103 if (drvp->PIO_mode > 3) 2104 drvp->PIO_mode = 3; 2105 } else 2106 goto error; 2107 } 2108 if (drvp->drive_flags & ATA_DRIVE_UDMA) 2109 wdccommand(chp, 0, SET_FEATURES, 0, 0, 0, 2110 0x40 | drvp->UDMA_mode, WDSF_SET_MODE); 2111 else 2112 if (drvp->drive_flags & ATA_DRIVE_DMA) 2113 wdccommand(chp, 0, SET_FEATURES, 0, 0, 0, 2114 0x20 | drvp->DMA_mode, WDSF_SET_MODE); 2115 else 2116 goto ready; 2117 errstring = "dmamode-atapi"; 2118 if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags, 2119 &tfd)) 2120 goto timeout; 2121 if (ATACH_ST(tfd) & WDCS_ERR) { 2122 if (ATACH_ERR(tfd) == WDCE_ABRT) { 2123 if (drvp->drive_flags & ATA_DRIVE_UDMA) 2124 goto error; 2125 else { 2126 /* 2127 * The drive rejected our DMA setting. 2128 * Fall back to mode 1. 2129 */ 2130 aprint_error_dev(atac->atac_dev, 2131 "channel %d drive %d:" 2132 " DMA mode %d rejected," 2133 " falling back to DMA mode 0\n", 2134 chp->ch_channel, xfer->c_drive, 2135 drvp->DMA_mode); 2136 if (drvp->DMA_mode > 0) 2137 drvp->DMA_mode = 0; 2138 } 2139 } else 2140 goto error; 2141 } 2142 ready: 2143 drvp->state = READY; 2144 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 2145 delay(10); /* some drives need a little delay here */ 2146 } 2147 /* start timeout machinery */ 2148 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) 2149 callout_reset(&chp->c_timo_callout, mstohz(sc_xfer->timeout), 2150 wdctimeout, chp); 2151 2152 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 2153 if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd) != 0) { 2154 aprint_error_dev(atac->atac_dev, "not ready, st = %02x\n", 2155 ATACH_ST(tfd)); 2156 sc_xfer->error = XS_TIMEOUT; 2157 return ATASTART_ABORT; 2158 } 2159 2160 /* 2161 * Even with WDCS_ERR, the device should accept a command packet 2162 * Limit length to what can be stuffed into the cylinder register 2163 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536, 2164 * but not all devices do that and it's not obvious from the 2165 * ATAPI spec that that behaviour should be expected. If more 2166 * data is necessary, multiple data transfer phases will be done. 2167 */ 2168 2169 wdccommand(chp, 0, ATAPI_PKT_CMD, 2170 xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff, 0, 0, 0, 2171 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0); 2172 2173 /* 2174 * If there is no interrupt for CMD input, busy-wait for it (done in 2175 * the interrupt routine. Poll routine will exit early in this case. 2176 */ 2177 if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) != 2178 ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) 2179 return ATASTART_POLL; 2180 else 2181 return ATASTART_STARTED; 2182 2183 timeout: 2184 aprint_error_dev(atac->atac_dev, "channel %d drive %d: %s timed out\n", 2185 chp->ch_channel, xfer->c_drive, errstring); 2186 sc_xfer->error = XS_TIMEOUT; 2187 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 2188 delay(10); /* some drives need a little delay here */ 2189 return ATASTART_ABORT; 2190 2191 error: 2192 aprint_error_dev(atac->atac_dev, 2193 "channel %d drive %d: %s error (0x%x)\n", 2194 chp->ch_channel, xfer->c_drive, errstring, ATACH_ERR(tfd)); 2195 sc_xfer->error = XS_SHORTSENSE; 2196 sc_xfer->sense.atapi_sense = ATACH_ERR(tfd); 2197 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 2198 delay(10); /* some drives need a little delay here */ 2199 return ATASTART_ABORT; 2200 } 2201 2202 static void 2203 mvsata_atapi_poll(struct ata_channel *chp, struct ata_xfer *xfer) 2204 { 2205 /* 2206 * If there is no interrupt for CMD input, busy-wait for it (done in 2207 * the interrupt routine. If it is a polled command, call the interrupt 2208 * routine until command is done. 2209 */ 2210 const bool poll = ((xfer->c_scsipi->xs_control & XS_CTL_POLL) != 0); 2211 2212 /* Wait for at last 400ns for status bit to be valid */ 2213 DELAY(1); 2214 mvsata_atapi_intr(chp, xfer, 0); 2215 2216 if (!poll) 2217 return; 2218 2219 if (chp->ch_flags & ATACH_DMA_WAIT) { 2220 wdc_dmawait(chp, xfer, xfer->c_scsipi->timeout); 2221 chp->ch_flags &= ~ATACH_DMA_WAIT; 2222 } 2223 2224 while ((xfer->c_scsipi->xs_status & XS_STS_DONE) == 0) { 2225 /* Wait for at last 400ns for status bit to be valid */ 2226 DELAY(1); 2227 mvsata_atapi_intr(chp, xfer, 0); 2228 } 2229 } 2230 2231 static int 2232 mvsata_atapi_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq) 2233 { 2234 struct mvsata_port *mvport = (struct mvsata_port *)chp; 2235 struct atac_softc *atac = chp->ch_atac; 2236 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 2237 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2238 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 2239 int len, phase, ire, error, retries=0, i; 2240 int tfd; 2241 void *cmd; 2242 2243 ata_channel_lock(chp); 2244 2245 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 2246 ("%s:%d:%d: mvsata_atapi_intr\n", 2247 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive)); 2248 2249 /* Is it not a transfer, but a control operation? */ 2250 if (drvp->state < READY) { 2251 aprint_error_dev(atac->atac_dev, 2252 "channel %d drive %d: bad state %d\n", 2253 chp->ch_channel, xfer->c_drive, drvp->state); 2254 panic("mvsata_atapi_intr: bad state"); 2255 } 2256 /* 2257 * If we missed an interrupt in a PIO transfer, reset and restart. 2258 * Don't try to continue transfer, we may have missed cycles. 2259 */ 2260 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) { 2261 ata_channel_unlock(chp); 2262 sc_xfer->error = XS_TIMEOUT; 2263 mvsata_atapi_reset(chp, xfer); 2264 return 1; 2265 } 2266 2267 /* Ack interrupt done in wdc_wait_for_unbusy */ 2268 MVSATA_WDC_WRITE_1(mvport, SRB_H, WDSD_IBM); 2269 if (wdc_wait_for_unbusy(chp, 2270 (irq == 0) ? sc_xfer->timeout : 0, AT_POLL, &tfd) == WDCWAIT_TOUT) { 2271 if (irq && (xfer->c_flags & C_TIMEOU) == 0) { 2272 ata_channel_unlock(chp); 2273 return 0; /* IRQ was not for us */ 2274 } 2275 aprint_error_dev(atac->atac_dev, 2276 "channel %d: device timeout, c_bcount=%d, c_skip=%d\n", 2277 chp->ch_channel, xfer->c_bcount, xfer->c_skip); 2278 if (xfer->c_flags & C_DMA) 2279 ata_dmaerr(drvp, 2280 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 2281 sc_xfer->error = XS_TIMEOUT; 2282 ata_channel_unlock(chp); 2283 mvsata_atapi_reset(chp, xfer); 2284 return 1; 2285 } 2286 2287 /* 2288 * If we missed an IRQ and were using DMA, flag it as a DMA error 2289 * and reset device. 2290 */ 2291 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) { 2292 ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0); 2293 sc_xfer->error = XS_RESET; 2294 ata_channel_unlock(chp); 2295 mvsata_atapi_reset(chp, xfer); 2296 return (1); 2297 } 2298 /* 2299 * if the request sense command was aborted, report the short sense 2300 * previously recorded, else continue normal processing 2301 */ 2302 2303 again: 2304 len = MVSATA_WDC_READ_1(mvport, SRB_LBAM) + 2305 256 * MVSATA_WDC_READ_1(mvport, SRB_LBAH); 2306 ire = MVSATA_WDC_READ_1(mvport, SRB_SC); 2307 phase = (ire & (WDCI_CMD | WDCI_IN)) | (ATACH_ST(tfd) & WDCS_DRQ); 2308 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, ( 2309 "mvsata_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x ire 0x%x :", 2310 xfer->c_bcount, len, ATACH_ST(tfd), ATACH_ERR(tfd), ire)); 2311 2312 switch (phase) { 2313 case PHASE_CMDOUT: 2314 cmd = sc_xfer->cmd; 2315 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, ("PHASE_CMDOUT\n")); 2316 /* Init the DMA channel if necessary */ 2317 if (xfer->c_flags & C_DMA) { 2318 error = mvsata_bdma_init(mvport, xfer); 2319 if (error) { 2320 if (error == EINVAL) { 2321 /* 2322 * We can't do DMA on this transfer 2323 * for some reason. Fall back to PIO. 2324 */ 2325 xfer->c_flags &= ~C_DMA; 2326 error = 0; 2327 } else { 2328 sc_xfer->error = XS_DRIVER_STUFFUP; 2329 break; 2330 } 2331 } 2332 } 2333 2334 /* send packet command */ 2335 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */ 2336 wdc->dataout_pio(chp, drvp->drive_flags, cmd, sc_xfer->cmdlen); 2337 2338 /* Start the DMA channel if necessary */ 2339 if (xfer->c_flags & C_DMA) { 2340 mvsata_bdma_start(mvport); 2341 chp->ch_flags |= ATACH_DMA_WAIT; 2342 } 2343 ata_channel_unlock(chp); 2344 return 1; 2345 2346 case PHASE_DATAOUT: 2347 /* write data */ 2348 DPRINTF(DEBUG_XFERS, ("PHASE_DATAOUT\n")); 2349 if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 || 2350 (xfer->c_flags & C_DMA) != 0) { 2351 aprint_error_dev(atac->atac_dev, 2352 "channel %d drive %d: bad data phase DATAOUT\n", 2353 chp->ch_channel, xfer->c_drive); 2354 if (xfer->c_flags & C_DMA) 2355 ata_dmaerr(drvp, 2356 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 2357 sc_xfer->error = XS_TIMEOUT; 2358 ata_channel_unlock(chp); 2359 mvsata_atapi_reset(chp, xfer); 2360 return 1; 2361 } 2362 xfer->c_atapi.c_lenoff = len - xfer->c_bcount; 2363 if (xfer->c_bcount < len) { 2364 aprint_error_dev(atac->atac_dev, "channel %d drive %d:" 2365 " warning: write only %d of %d requested bytes\n", 2366 chp->ch_channel, xfer->c_drive, xfer->c_bcount, 2367 len); 2368 len = xfer->c_bcount; 2369 } 2370 2371 wdc->dataout_pio(chp, drvp->drive_flags, 2372 (char *)xfer->c_databuf + xfer->c_skip, len); 2373 2374 for (i = xfer->c_atapi.c_lenoff; i > 0; i -= 2) 2375 MVSATA_WDC_WRITE_2(mvport, SRB_PIOD, 0); 2376 2377 xfer->c_skip += len; 2378 xfer->c_bcount -= len; 2379 ata_channel_unlock(chp); 2380 return 1; 2381 2382 case PHASE_DATAIN: 2383 /* Read data */ 2384 DPRINTF(DEBUG_XFERS, ("PHASE_DATAIN\n")); 2385 if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 || 2386 (xfer->c_flags & C_DMA) != 0) { 2387 aprint_error_dev(atac->atac_dev, 2388 "channel %d drive %d: bad data phase DATAIN\n", 2389 chp->ch_channel, xfer->c_drive); 2390 if (xfer->c_flags & C_DMA) 2391 ata_dmaerr(drvp, 2392 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 2393 ata_channel_unlock(chp); 2394 sc_xfer->error = XS_TIMEOUT; 2395 mvsata_atapi_reset(chp, xfer); 2396 return 1; 2397 } 2398 xfer->c_atapi.c_lenoff = len - xfer->c_bcount; 2399 if (xfer->c_bcount < len) { 2400 aprint_error_dev(atac->atac_dev, "channel %d drive %d:" 2401 " warning: reading only %d of %d bytes\n", 2402 chp->ch_channel, xfer->c_drive, xfer->c_bcount, 2403 len); 2404 len = xfer->c_bcount; 2405 } 2406 2407 wdc->datain_pio(chp, drvp->drive_flags, 2408 (char *)xfer->c_databuf + xfer->c_skip, len); 2409 2410 if (xfer->c_atapi.c_lenoff > 0) 2411 wdcbit_bucket(chp, len - xfer->c_bcount); 2412 2413 xfer->c_skip += len; 2414 xfer->c_bcount -= len; 2415 ata_channel_unlock(chp); 2416 return 1; 2417 2418 case PHASE_ABORTED: 2419 case PHASE_COMPLETED: 2420 DPRINTF(DEBUG_XFERS, ("PHASE_COMPLETED\n")); 2421 if (xfer->c_flags & C_DMA) 2422 xfer->c_bcount -= sc_xfer->datalen; 2423 sc_xfer->resid = xfer->c_bcount; 2424 /* this will unlock channel lock too */ 2425 mvsata_atapi_phase_complete(xfer, tfd); 2426 return 1; 2427 2428 default: 2429 if (++retries<500) { 2430 DELAY(100); 2431 tfd = ATACH_ERR_ST( 2432 MVSATA_WDC_READ_1(mvport, SRB_FE), 2433 MVSATA_WDC_READ_1(mvport, SRB_CS) 2434 ); 2435 goto again; 2436 } 2437 aprint_error_dev(atac->atac_dev, 2438 "channel %d drive %d: unknown phase 0x%x\n", 2439 chp->ch_channel, xfer->c_drive, phase); 2440 if (ATACH_ST(tfd) & WDCS_ERR) { 2441 sc_xfer->error = XS_SHORTSENSE; 2442 sc_xfer->sense.atapi_sense = ATACH_ERR(tfd); 2443 } else { 2444 if (xfer->c_flags & C_DMA) 2445 ata_dmaerr(drvp, 2446 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 2447 sc_xfer->error = XS_RESET; 2448 ata_channel_unlock(chp); 2449 mvsata_atapi_reset(chp, xfer); 2450 return (1); 2451 } 2452 } 2453 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 2454 ("mvsata_atapi_intr: %s (end), error 0x%x " 2455 "sense 0x%x\n", __func__, 2456 sc_xfer->error, sc_xfer->sense.atapi_sense)); 2457 ata_channel_unlock(chp); 2458 mvsata_atapi_done(chp, xfer); 2459 return 1; 2460 } 2461 2462 static void 2463 mvsata_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, 2464 int reason) 2465 { 2466 struct mvsata_port *mvport = (struct mvsata_port *)chp; 2467 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2468 bool deactivate = true; 2469 2470 /* remove this command from xfer queue */ 2471 switch (reason) { 2472 case KILL_GONE_INACTIVE: 2473 deactivate = false; 2474 /* FALLTHROUGH */ 2475 case KILL_GONE: 2476 sc_xfer->error = XS_DRIVER_STUFFUP; 2477 break; 2478 case KILL_RESET: 2479 sc_xfer->error = XS_RESET; 2480 break; 2481 case KILL_REQUEUE: 2482 sc_xfer->error = XS_REQUEUE; 2483 break; 2484 default: 2485 aprint_error_dev(MVSATA_DEV2(mvport), 2486 "mvsata_atapi_kill_xfer: unknown reason %d\n", reason); 2487 panic("mvsata_atapi_kill_xfer"); 2488 } 2489 2490 if (deactivate) 2491 ata_deactivate_xfer(chp, xfer); 2492 2493 ata_free_xfer(chp, xfer); 2494 scsipi_done(sc_xfer); 2495 } 2496 2497 static void 2498 mvsata_atapi_reset(struct ata_channel *chp, struct ata_xfer *xfer) 2499 { 2500 struct mvsata_port *mvport = (struct mvsata_port *)chp; 2501 struct atac_softc *atac = chp->ch_atac; 2502 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 2503 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2504 int tfd; 2505 2506 ata_channel_lock(chp); 2507 2508 mvsata_pmp_select(mvport, xfer->c_drive); 2509 2510 wdccommandshort(chp, 0, ATAPI_SOFT_RESET); 2511 drvp->state = 0; 2512 if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL, &tfd) != 0) { 2513 printf("%s:%d:%d: reset failed\n", device_xname(atac->atac_dev), 2514 chp->ch_channel, xfer->c_drive); 2515 sc_xfer->error = XS_SELTIMEOUT; 2516 } 2517 2518 ata_channel_unlock(chp); 2519 2520 mvsata_atapi_done(chp, xfer); 2521 return; 2522 } 2523 2524 static void 2525 mvsata_atapi_phase_complete(struct ata_xfer *xfer, int tfd) 2526 { 2527 struct ata_channel *chp = xfer->c_chp; 2528 struct atac_softc *atac = chp->ch_atac; 2529 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 2530 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2531 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 2532 2533 ata_channel_lock_owned(chp); 2534 2535 /* wait for DSC if needed */ 2536 if (drvp->drive_flags & ATA_DRIVE_ATAPIDSCW) { 2537 DPRINTF(DEBUG_XFERS, 2538 ("%s:%d:%d: mvsata_atapi_phase_complete: polldsc %d\n", 2539 device_xname(atac->atac_dev), chp->ch_channel, 2540 xfer->c_drive, xfer->c_atapi.c_dscpoll)); 2541 if (cold) 2542 panic("mvsata_atapi_phase_complete: cold"); 2543 2544 if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10, AT_POLL, &tfd) == 2545 WDCWAIT_TOUT) { 2546 /* 10ms not enough, try again in 1 tick */ 2547 if (xfer->c_atapi.c_dscpoll++ > 2548 mstohz(sc_xfer->timeout)) { 2549 aprint_error_dev(atac->atac_dev, 2550 "channel %d: wait_for_dsc failed\n", 2551 chp->ch_channel); 2552 ata_channel_unlock(chp); 2553 sc_xfer->error = XS_TIMEOUT; 2554 mvsata_atapi_reset(chp, xfer); 2555 } else { 2556 callout_reset(&chp->c_timo_callout, 1, 2557 mvsata_atapi_polldsc, chp); 2558 ata_channel_unlock(chp); 2559 } 2560 return; 2561 } 2562 } 2563 2564 /* 2565 * Some drive occasionally set WDCS_ERR with 2566 * "ATA illegal length indication" in the error 2567 * register. If we read some data the sense is valid 2568 * anyway, so don't report the error. 2569 */ 2570 if (ATACH_ST(tfd) & WDCS_ERR && 2571 ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 || 2572 sc_xfer->resid == sc_xfer->datalen)) { 2573 /* save the short sense */ 2574 sc_xfer->error = XS_SHORTSENSE; 2575 sc_xfer->sense.atapi_sense = ATACH_ERR(tfd); 2576 if ((sc_xfer->xs_periph->periph_quirks & PQUIRK_NOSENSE) == 0) { 2577 /* ask scsipi to send a REQUEST_SENSE */ 2578 sc_xfer->error = XS_BUSY; 2579 sc_xfer->status = SCSI_CHECK; 2580 } else 2581 if (wdc->dma_status & (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) { 2582 ata_dmaerr(drvp, 2583 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 2584 sc_xfer->error = XS_RESET; 2585 ata_channel_unlock(chp); 2586 mvsata_atapi_reset(chp, xfer); 2587 return; 2588 } 2589 } 2590 if (xfer->c_bcount != 0) { 2591 DPRINTF(DEBUG_XFERS, ("%s:%d:%d: mvsata_atapi_intr:" 2592 " bcount value is %d after io\n", 2593 device_xname(atac->atac_dev), chp->ch_channel, 2594 xfer->c_drive, xfer->c_bcount)); 2595 } 2596 #ifdef DIAGNOSTIC 2597 if (xfer->c_bcount < 0) { 2598 aprint_error_dev(atac->atac_dev, 2599 "channel %d drive %d: mvsata_atapi_intr:" 2600 " warning: bcount value is %d after io\n", 2601 chp->ch_channel, xfer->c_drive, xfer->c_bcount); 2602 } 2603 #endif 2604 2605 DPRINTF(DEBUG_XFERS, 2606 ("%s:%d:%d: mvsata_atapi_phase_complete:" 2607 " mvsata_atapi_done(), error 0x%x sense 0x%x\n", 2608 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 2609 sc_xfer->error, sc_xfer->sense.atapi_sense)); 2610 ata_channel_unlock(chp); 2611 mvsata_atapi_done(chp, xfer); 2612 } 2613 2614 static void 2615 mvsata_atapi_done(struct ata_channel *chp, struct ata_xfer *xfer) 2616 { 2617 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2618 bool iserror = (sc_xfer->error != XS_NOERROR); 2619 2620 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 2621 ("%s:%d:%d: mvsata_atapi_done: flags 0x%x\n", 2622 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 2623 xfer->c_drive, (u_int)xfer->c_flags)); 2624 2625 if (ata_waitdrain_xfer_check(chp, xfer)) 2626 return; 2627 2628 /* mark controller inactive and free the command */ 2629 ata_deactivate_xfer(chp, xfer); 2630 2631 ata_free_xfer(chp, xfer); 2632 2633 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 2634 ("%s:%d: mvsata_atapi_done: scsipi_done\n", 2635 device_xname(chp->ch_atac->atac_dev), chp->ch_channel)); 2636 scsipi_done(sc_xfer); 2637 DPRINTF(DEBUG_FUNCS, 2638 ("%s:%d: atastart from wdc_atapi_done, flags 0x%x\n", 2639 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 2640 chp->ch_flags)); 2641 if (!iserror) 2642 atastart(chp); 2643 } 2644 2645 static void 2646 mvsata_atapi_polldsc(void *arg) 2647 { 2648 struct ata_channel *chp = arg; 2649 struct ata_xfer *xfer = ata_queue_get_active_xfer(chp); 2650 2651 KASSERT(xfer != NULL); 2652 2653 ata_channel_lock(chp); 2654 2655 /* this will unlock channel lock too */ 2656 mvsata_atapi_phase_complete(xfer, 0); 2657 } 2658 #endif /* NATAPIBUS > 0 */ 2659 2660 2661 /* 2662 * XXXX: Shall we need lock for race condition in mvsata_edma_enqueue{,_gen2}(), 2663 * if supported queuing command by atabus? The race condition will not happen 2664 * if this is called only to the thread of atabus. 2665 */ 2666 static int 2667 mvsata_edma_enqueue(struct mvsata_port *mvport, struct ata_xfer *xfer) 2668 { 2669 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 2670 struct ata_bio *ata_bio = &xfer->c_bio; 2671 void *databuf = (uint8_t *)xfer->c_databuf + xfer->c_skip; 2672 struct eprd *eprd; 2673 bus_addr_t crqb_base_addr; 2674 bus_dmamap_t data_dmamap; 2675 uint32_t reg; 2676 int erqqip, erqqop, next, rv, i; 2677 2678 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, ("%s:%d:%d: mvsata_edma_enqueue:" 2679 " blkno=0x%" PRIx64 ", nbytes=%d, flags=0x%x\n", 2680 device_xname(MVSATA_DEV2(mvport)), mvport->port_hc->hc, 2681 mvport->port, ata_bio->blkno, ata_bio->nbytes, ata_bio->flags)); 2682 2683 reg = MVSATA_EDMA_READ_4(mvport, EDMA_REQQOP); 2684 erqqop = (reg & EDMA_REQQP_ERQQP_MASK) >> EDMA_REQQP_ERQQP_SHIFT; 2685 reg = MVSATA_EDMA_READ_4(mvport, EDMA_REQQIP); 2686 erqqip = (reg & EDMA_REQQP_ERQQP_MASK) >> EDMA_REQQP_ERQQP_SHIFT; 2687 next = erqqip; 2688 MVSATA_EDMAQ_INC(next); 2689 if (next == erqqop) { 2690 /* queue full */ 2691 return EBUSY; 2692 } 2693 DPRINTF(DEBUG_XFERS, 2694 (" erqqip=%d, quetag=%d\n", erqqip, xfer->c_slot)); 2695 2696 rv = mvsata_dma_bufload(mvport, xfer->c_slot, databuf, ata_bio->nbytes, 2697 ata_bio->flags); 2698 if (rv != 0) 2699 return rv; 2700 2701 /* setup EDMA Physical Region Descriptors (ePRD) Table Data */ 2702 data_dmamap = mvport->port_reqtbl[xfer->c_slot].data_dmamap; 2703 eprd = mvport->port_reqtbl[xfer->c_slot].eprd; 2704 for (i = 0; i < data_dmamap->dm_nsegs; i++) { 2705 bus_addr_t ds_addr = data_dmamap->dm_segs[i].ds_addr; 2706 bus_size_t ds_len = data_dmamap->dm_segs[i].ds_len; 2707 2708 eprd->prdbal = htole32(ds_addr & EPRD_PRDBAL_MASK); 2709 eprd->bytecount = htole32(EPRD_BYTECOUNT(ds_len)); 2710 eprd->eot = htole16(0); 2711 eprd->prdbah = htole32((ds_addr >> 16) >> 16); 2712 eprd++; 2713 } 2714 (eprd - 1)->eot |= htole16(EPRD_EOT); 2715 #ifdef MVSATA_DEBUG 2716 if (mvsata_debug >= 3) 2717 mvsata_print_eprd(mvport, xfer->c_slot); 2718 #endif 2719 bus_dmamap_sync(mvport->port_dmat, mvport->port_eprd_dmamap, 2720 mvport->port_reqtbl[xfer->c_slot].eprd_offset, MVSATA_EPRD_MAX_SIZE, 2721 BUS_DMASYNC_PREWRITE); 2722 2723 /* setup EDMA Command Request Block (CRQB) Data */ 2724 sc->sc_edma_setup_crqb(mvport, erqqip, xfer); 2725 #ifdef MVSATA_DEBUG 2726 if (mvsata_debug >= 3) 2727 mvsata_print_crqb(mvport, erqqip); 2728 #endif 2729 bus_dmamap_sync(mvport->port_dmat, mvport->port_crqb_dmamap, 2730 erqqip * sizeof(union mvsata_crqb), 2731 sizeof(union mvsata_crqb), BUS_DMASYNC_PREWRITE); 2732 2733 MVSATA_EDMAQ_INC(erqqip); 2734 2735 crqb_base_addr = mvport->port_crqb_dmamap->dm_segs[0].ds_addr & 2736 (EDMA_REQQP_ERQQBAP_MASK | EDMA_REQQP_ERQQBA_MASK); 2737 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQBAH, (crqb_base_addr >> 16) >> 16); 2738 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQIP, 2739 crqb_base_addr | (erqqip << EDMA_REQQP_ERQQP_SHIFT)); 2740 2741 return 0; 2742 } 2743 2744 static int 2745 mvsata_edma_handle(struct mvsata_port *mvport, struct ata_xfer *xfer1) 2746 { 2747 struct ata_channel *chp = &mvport->port_ata_channel; 2748 struct crpb *crpb; 2749 struct ata_bio *ata_bio; 2750 struct ata_xfer *xfer; 2751 uint32_t reg; 2752 int erqqop, erpqip, erpqop, prev_erpqop, quetag, handled = 0, n; 2753 int st, dmaerr; 2754 2755 /* First, Sync for Request Queue buffer */ 2756 reg = MVSATA_EDMA_READ_4(mvport, EDMA_REQQOP); 2757 erqqop = (reg & EDMA_REQQP_ERQQP_MASK) >> EDMA_REQQP_ERQQP_SHIFT; 2758 if (mvport->port_prev_erqqop != erqqop) { 2759 const int s = sizeof(union mvsata_crqb); 2760 2761 if (mvport->port_prev_erqqop < erqqop) 2762 n = erqqop - mvport->port_prev_erqqop; 2763 else { 2764 if (erqqop > 0) 2765 bus_dmamap_sync(mvport->port_dmat, 2766 mvport->port_crqb_dmamap, 0, erqqop * s, 2767 BUS_DMASYNC_POSTWRITE); 2768 n = MVSATA_EDMAQ_LEN - mvport->port_prev_erqqop; 2769 } 2770 if (n > 0) 2771 bus_dmamap_sync(mvport->port_dmat, 2772 mvport->port_crqb_dmamap, 2773 mvport->port_prev_erqqop * s, n * s, 2774 BUS_DMASYNC_POSTWRITE); 2775 mvport->port_prev_erqqop = erqqop; 2776 } 2777 2778 reg = MVSATA_EDMA_READ_4(mvport, EDMA_RESQIP); 2779 erpqip = (reg & EDMA_RESQP_ERPQP_MASK) >> EDMA_RESQP_ERPQP_SHIFT; 2780 reg = MVSATA_EDMA_READ_4(mvport, EDMA_RESQOP); 2781 erpqop = (reg & EDMA_RESQP_ERPQP_MASK) >> EDMA_RESQP_ERPQP_SHIFT; 2782 2783 DPRINTF(DEBUG_XFERS, 2784 ("%s:%d:%d: mvsata_edma_handle: erpqip=%d, erpqop=%d\n", 2785 device_xname(MVSATA_DEV2(mvport)), mvport->port_hc->hc, 2786 mvport->port, erpqip, erpqop)); 2787 2788 if (erpqop == erpqip) 2789 return 0; 2790 2791 if (erpqop < erpqip) 2792 n = erpqip - erpqop; 2793 else { 2794 if (erpqip > 0) 2795 bus_dmamap_sync(mvport->port_dmat, 2796 mvport->port_crpb_dmamap, 2797 0, erpqip * sizeof(struct crpb), 2798 BUS_DMASYNC_POSTREAD); 2799 n = MVSATA_EDMAQ_LEN - erpqop; 2800 } 2801 if (n > 0) 2802 bus_dmamap_sync(mvport->port_dmat, mvport->port_crpb_dmamap, 2803 erpqop * sizeof(struct crpb), 2804 n * sizeof(struct crpb), BUS_DMASYNC_POSTREAD); 2805 2806 uint32_t aslots = ata_queue_active(chp); 2807 2808 prev_erpqop = erpqop; 2809 while (erpqop != erpqip) { 2810 #ifdef MVSATA_DEBUG 2811 if (mvsata_debug >= 3) 2812 mvsata_print_crpb(mvport, erpqop); 2813 #endif 2814 crpb = mvport->port_crpb + erpqop; 2815 MVSATA_EDMAQ_INC(erpqop); 2816 2817 quetag = CRPB_CHOSTQUETAG(le16toh(crpb->id)); 2818 2819 if ((aslots & __BIT(quetag)) == 0) { 2820 /* not actually executing */ 2821 continue; 2822 } 2823 2824 xfer = ata_queue_hwslot_to_xfer(chp, quetag); 2825 2826 bus_dmamap_sync(mvport->port_dmat, mvport->port_eprd_dmamap, 2827 mvport->port_reqtbl[xfer->c_slot].eprd_offset, 2828 MVSATA_EPRD_MAX_SIZE, BUS_DMASYNC_POSTWRITE); 2829 2830 st = CRPB_CDEVSTS(le16toh(crpb->rspflg)); 2831 dmaerr = CRPB_CEDMASTS(le16toh(crpb->rspflg)); 2832 2833 ata_bio = &xfer->c_bio; 2834 ata_bio->error = NOERROR; 2835 if (dmaerr != 0) 2836 ata_bio->error = ERR_DMA; 2837 2838 mvsata_dma_bufunload(mvport, quetag, ata_bio->flags); 2839 2840 KASSERT(xfer->c_flags & C_DMA); 2841 mvsata_bio_intr(chp, xfer, ATACH_ERR_ST(0, st)); 2842 2843 if (xfer1 == NULL) 2844 handled++; 2845 else if (xfer == xfer1) { 2846 handled = 1; 2847 break; 2848 } 2849 } 2850 if (prev_erpqop < erpqop) 2851 n = erpqop - prev_erpqop; 2852 else { 2853 if (erpqop > 0) 2854 bus_dmamap_sync(mvport->port_dmat, 2855 mvport->port_crpb_dmamap, 0, 2856 erpqop * sizeof(struct crpb), BUS_DMASYNC_PREREAD); 2857 n = MVSATA_EDMAQ_LEN - prev_erpqop; 2858 } 2859 if (n > 0) 2860 bus_dmamap_sync(mvport->port_dmat, mvport->port_crpb_dmamap, 2861 prev_erpqop * sizeof(struct crpb), 2862 n * sizeof(struct crpb), BUS_DMASYNC_PREREAD); 2863 2864 reg &= ~EDMA_RESQP_ERPQP_MASK; 2865 reg |= (erpqop << EDMA_RESQP_ERPQP_SHIFT); 2866 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQOP, reg); 2867 2868 return handled; 2869 } 2870 2871 static int 2872 mvsata_edma_wait(struct mvsata_port *mvport, struct ata_xfer *xfer, int timeout) 2873 { 2874 int xtime; 2875 2876 for (xtime = 0; xtime < timeout * 10; xtime++) { 2877 if (mvsata_edma_handle(mvport, xfer)) 2878 return 0; 2879 DELAY(100); 2880 } 2881 2882 DPRINTF(DEBUG_FUNCS, ("%s: timeout: %p\n", __func__, xfer)); 2883 mvsata_edma_rqq_remove(mvport, xfer); 2884 xfer->c_flags |= C_TIMEOU; 2885 return 1; 2886 } 2887 2888 static void 2889 mvsata_edma_rqq_remove(struct mvsata_port *mvport, struct ata_xfer *xfer) 2890 { 2891 struct ata_channel *chp = &mvport->port_ata_channel; 2892 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 2893 bus_addr_t crqb_base_addr; 2894 int erqqip, i; 2895 2896 /* First, hardware reset, stop EDMA */ 2897 mvsata_hreset_port(mvport); 2898 2899 /* cleanup completed EDMA safely */ 2900 mvsata_edma_handle(mvport, NULL); 2901 2902 bus_dmamap_sync(mvport->port_dmat, mvport->port_crqb_dmamap, 0, 2903 sizeof(union mvsata_crqb) * MVSATA_EDMAQ_LEN, BUS_DMASYNC_PREWRITE); 2904 2905 uint32_t aslots = ata_queue_active(chp); 2906 2907 for (i = 0, erqqip = 0; i < MVSATA_EDMAQ_LEN; i++) { 2908 struct ata_xfer *rqxfer; 2909 2910 if ((aslots & __BIT(i)) == 0) 2911 continue; 2912 2913 if (i == xfer->c_slot) { 2914 /* remove xfer from EDMA request queue */ 2915 bus_dmamap_sync(mvport->port_dmat, 2916 mvport->port_eprd_dmamap, 2917 mvport->port_reqtbl[i].eprd_offset, 2918 MVSATA_EPRD_MAX_SIZE, BUS_DMASYNC_POSTWRITE); 2919 mvsata_dma_bufunload(mvport, i, xfer->c_bio.flags); 2920 /* quetag freed by caller later */ 2921 continue; 2922 } 2923 2924 rqxfer = ata_queue_hwslot_to_xfer(chp, i); 2925 sc->sc_edma_setup_crqb(mvport, erqqip, rqxfer); 2926 erqqip++; 2927 } 2928 bus_dmamap_sync(mvport->port_dmat, mvport->port_crqb_dmamap, 0, 2929 sizeof(union mvsata_crqb) * MVSATA_EDMAQ_LEN, 2930 BUS_DMASYNC_POSTWRITE); 2931 2932 mvsata_edma_config(mvport, mvport->port_edmamode_curr); 2933 mvsata_edma_reset_qptr(mvport); 2934 mvsata_edma_enable(mvport); 2935 2936 crqb_base_addr = mvport->port_crqb_dmamap->dm_segs[0].ds_addr & 2937 (EDMA_REQQP_ERQQBAP_MASK | EDMA_REQQP_ERQQBA_MASK); 2938 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQBAH, (crqb_base_addr >> 16) >> 16); 2939 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQIP, 2940 crqb_base_addr | (erqqip << EDMA_REQQP_ERQQP_SHIFT)); 2941 } 2942 2943 #if NATAPIBUS > 0 2944 static int 2945 mvsata_bdma_init(struct mvsata_port *mvport, struct ata_xfer *xfer) 2946 { 2947 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 2948 struct eprd *eprd; 2949 bus_dmamap_t data_dmamap; 2950 bus_addr_t eprd_addr; 2951 int i, rv; 2952 void *databuf = (uint8_t *)xfer->c_databuf + xfer->c_skip; 2953 2954 DPRINTF(DEBUG_FUNCS|DEBUG_XFERS, 2955 ("%s:%d:%d: mvsata_bdma_init: datalen=%d, xs_control=0x%x\n", 2956 device_xname(MVSATA_DEV2(mvport)), mvport->port_hc->hc, 2957 mvport->port, sc_xfer->datalen, sc_xfer->xs_control)); 2958 2959 rv = mvsata_dma_bufload(mvport, xfer->c_slot, databuf, 2960 sc_xfer->datalen, 2961 sc_xfer->xs_control & XS_CTL_DATA_IN ? ATA_READ : 0); 2962 if (rv != 0) 2963 return rv; 2964 2965 /* setup EDMA Physical Region Descriptors (ePRD) Table Data */ 2966 data_dmamap = mvport->port_reqtbl[xfer->c_slot].data_dmamap; 2967 eprd = mvport->port_reqtbl[xfer->c_slot].eprd; 2968 for (i = 0; i < data_dmamap->dm_nsegs; i++) { 2969 bus_addr_t ds_addr = data_dmamap->dm_segs[i].ds_addr; 2970 bus_size_t ds_len = data_dmamap->dm_segs[i].ds_len; 2971 2972 eprd->prdbal = htole32(ds_addr & EPRD_PRDBAL_MASK); 2973 eprd->bytecount = htole32(EPRD_BYTECOUNT(ds_len)); 2974 eprd->eot = htole16(0); 2975 eprd->prdbah = htole32((ds_addr >> 16) >> 16); 2976 eprd++; 2977 } 2978 (eprd - 1)->eot |= htole16(EPRD_EOT); 2979 #ifdef MVSATA_DEBUG 2980 if (mvsata_debug >= 3) 2981 mvsata_print_eprd(mvport, xfer->c_slot); 2982 #endif 2983 bus_dmamap_sync(mvport->port_dmat, mvport->port_eprd_dmamap, 2984 mvport->port_reqtbl[xfer->c_slot].eprd_offset, 2985 MVSATA_EPRD_MAX_SIZE, BUS_DMASYNC_PREWRITE); 2986 eprd_addr = mvport->port_eprd_dmamap->dm_segs[0].ds_addr + 2987 mvport->port_reqtbl[xfer->c_slot].eprd_offset; 2988 2989 MVSATA_EDMA_WRITE_4(mvport, DMA_DTLBA, eprd_addr & DMA_DTLBA_MASK); 2990 MVSATA_EDMA_WRITE_4(mvport, DMA_DTHBA, (eprd_addr >> 16) >> 16); 2991 2992 if (sc_xfer->xs_control & XS_CTL_DATA_IN) 2993 MVSATA_EDMA_WRITE_4(mvport, DMA_C, DMA_C_READ); 2994 else 2995 MVSATA_EDMA_WRITE_4(mvport, DMA_C, 0); 2996 2997 return 0; 2998 } 2999 3000 static void 3001 mvsata_bdma_start(struct mvsata_port *mvport) 3002 { 3003 3004 #ifdef MVSATA_DEBUG 3005 if (mvsata_debug >= 3) 3006 mvsata_print_eprd(mvport, 0); 3007 #endif 3008 3009 MVSATA_EDMA_WRITE_4(mvport, DMA_C, 3010 MVSATA_EDMA_READ_4(mvport, DMA_C) | DMA_C_START); 3011 } 3012 #endif 3013 #endif 3014 3015 3016 static int 3017 mvsata_port_init(struct mvsata_hc *mvhc, int port) 3018 { 3019 struct mvsata_softc *sc = mvhc->hc_sc; 3020 struct mvsata_port *mvport; 3021 struct ata_channel *chp; 3022 int channel, rv, i; 3023 const int crqbq_size = sizeof(union mvsata_crqb) * MVSATA_EDMAQ_LEN; 3024 const int crpbq_size = sizeof(struct crpb) * MVSATA_EDMAQ_LEN; 3025 const int eprd_buf_size = MVSATA_EPRD_MAX_SIZE * MVSATA_EDMAQ_LEN; 3026 3027 mvport = malloc(sizeof(struct mvsata_port), M_DEVBUF, 3028 M_ZERO | M_NOWAIT); 3029 if (mvport == NULL) { 3030 aprint_error("%s:%d: can't allocate memory for port %d\n", 3031 device_xname(MVSATA_DEV(sc)), mvhc->hc, port); 3032 return ENOMEM; 3033 } 3034 3035 mvport->port = port; 3036 mvport->port_hc = mvhc; 3037 mvport->port_edmamode_negotiated = nodma; 3038 3039 rv = bus_space_subregion(mvhc->hc_iot, mvhc->hc_ioh, 3040 EDMA_REGISTERS_OFFSET + port * EDMA_REGISTERS_SIZE, 3041 EDMA_REGISTERS_SIZE, &mvport->port_ioh); 3042 if (rv != 0) { 3043 aprint_error("%s:%d: can't subregion EDMA %d registers\n", 3044 device_xname(MVSATA_DEV(sc)), mvhc->hc, port); 3045 goto fail0; 3046 } 3047 mvport->port_iot = mvhc->hc_iot; 3048 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, SATA_SS, 4, 3049 &mvport->port_sata_sstatus); 3050 if (rv != 0) { 3051 aprint_error("%s:%d:%d: couldn't subregion sstatus regs\n", 3052 device_xname(MVSATA_DEV(sc)), mvhc->hc, port); 3053 goto fail0; 3054 } 3055 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, SATA_SE, 4, 3056 &mvport->port_sata_serror); 3057 if (rv != 0) { 3058 aprint_error("%s:%d:%d: couldn't subregion serror regs\n", 3059 device_xname(MVSATA_DEV(sc)), mvhc->hc, port); 3060 goto fail0; 3061 } 3062 if (sc->sc_rev == gen1) 3063 rv = bus_space_subregion(mvhc->hc_iot, mvhc->hc_ioh, 3064 SATAHC_I_R02(port), 4, &mvport->port_sata_scontrol); 3065 else 3066 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, 3067 SATA_SC, 4, &mvport->port_sata_scontrol); 3068 if (rv != 0) { 3069 aprint_error("%s:%d:%d: couldn't subregion scontrol regs\n", 3070 device_xname(MVSATA_DEV(sc)), mvhc->hc, port); 3071 goto fail0; 3072 } 3073 mvport->port_dmat = sc->sc_dmat; 3074 mvhc->hc_ports[port] = mvport; 3075 3076 channel = mvhc->hc * sc->sc_port + port; 3077 chp = &mvport->port_ata_channel; 3078 chp->ch_channel = channel; 3079 chp->ch_atac = &sc->sc_wdcdev.sc_atac; 3080 chp->ch_queue = ata_queue_alloc(MVSATA_EDMAQ_LEN); 3081 sc->sc_ata_channels[channel] = chp; 3082 3083 rv = mvsata_wdc_reg_init(mvport, sc->sc_wdcdev.regs + channel); 3084 if (rv != 0) 3085 goto fail0; 3086 3087 rv = bus_dmamap_create(mvport->port_dmat, crqbq_size, 1, crqbq_size, 0, 3088 BUS_DMA_NOWAIT, &mvport->port_crqb_dmamap); 3089 if (rv != 0) { 3090 aprint_error( 3091 "%s:%d:%d: EDMA CRQB map create failed: error=%d\n", 3092 device_xname(MVSATA_DEV(sc)), mvhc->hc, port, rv); 3093 goto fail0; 3094 } 3095 rv = bus_dmamap_create(mvport->port_dmat, crpbq_size, 1, crpbq_size, 0, 3096 BUS_DMA_NOWAIT, &mvport->port_crpb_dmamap); 3097 if (rv != 0) { 3098 aprint_error( 3099 "%s:%d:%d: EDMA CRPB map create failed: error=%d\n", 3100 device_xname(MVSATA_DEV(sc)), mvhc->hc, port, rv); 3101 goto fail1; 3102 } 3103 rv = bus_dmamap_create(mvport->port_dmat, eprd_buf_size, 1, 3104 eprd_buf_size, 0, BUS_DMA_NOWAIT, &mvport->port_eprd_dmamap); 3105 if (rv != 0) { 3106 aprint_error( 3107 "%s:%d:%d: EDMA ePRD buffer map create failed: error=%d\n", 3108 device_xname(MVSATA_DEV(sc)), mvhc->hc, port, rv); 3109 goto fail2; 3110 } 3111 for (i = 0; i < MVSATA_EDMAQ_LEN; i++) { 3112 rv = bus_dmamap_create(mvport->port_dmat, MAXPHYS, 3113 MAXPHYS / PAGE_SIZE, MAXPHYS, 0, BUS_DMA_NOWAIT, 3114 &mvport->port_reqtbl[i].data_dmamap); 3115 if (rv != 0) { 3116 aprint_error("%s:%d:%d:" 3117 " EDMA data map(%d) create failed: error=%d\n", 3118 device_xname(MVSATA_DEV(sc)), mvhc->hc, port, i, 3119 rv); 3120 goto fail3; 3121 } 3122 } 3123 3124 return 0; 3125 3126 fail3: 3127 for (i--; i >= 0; i--) 3128 bus_dmamap_destroy(mvport->port_dmat, 3129 mvport->port_reqtbl[i].data_dmamap); 3130 bus_dmamap_destroy(mvport->port_dmat, mvport->port_eprd_dmamap); 3131 fail2: 3132 bus_dmamap_destroy(mvport->port_dmat, mvport->port_crpb_dmamap); 3133 fail1: 3134 bus_dmamap_destroy(mvport->port_dmat, mvport->port_crqb_dmamap); 3135 fail0: 3136 return rv; 3137 } 3138 3139 static int 3140 mvsata_wdc_reg_init(struct mvsata_port *mvport, struct wdc_regs *wdr) 3141 { 3142 int hc, port, rv, i; 3143 3144 hc = mvport->port_hc->hc; 3145 port = mvport->port; 3146 3147 /* Create subregion for Shadow Registers Map */ 3148 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, 3149 SHADOW_REG_BLOCK_OFFSET, SHADOW_REG_BLOCK_SIZE, &wdr->cmd_baseioh); 3150 if (rv != 0) { 3151 aprint_error("%s:%d:%d: couldn't subregion shadow block regs\n", 3152 device_xname(MVSATA_DEV2(mvport)), hc, port); 3153 return rv; 3154 } 3155 wdr->cmd_iot = mvport->port_iot; 3156 3157 /* Once create subregion for each command registers */ 3158 for (i = 0; i < WDC_NREG; i++) { 3159 rv = bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 3160 i * 4, sizeof(uint32_t), &wdr->cmd_iohs[i]); 3161 if (rv != 0) { 3162 aprint_error("%s:%d:%d: couldn't subregion cmd regs\n", 3163 device_xname(MVSATA_DEV2(mvport)), hc, port); 3164 return rv; 3165 } 3166 } 3167 /* Create subregion for Alternate Status register */ 3168 rv = bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 3169 i * 4, sizeof(uint32_t), &wdr->ctl_ioh); 3170 if (rv != 0) { 3171 aprint_error("%s:%d:%d: couldn't subregion cmd regs\n", 3172 device_xname(MVSATA_DEV2(mvport)), hc, port); 3173 return rv; 3174 } 3175 wdr->ctl_iot = mvport->port_iot; 3176 3177 wdc_init_shadow_regs(wdr); 3178 3179 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, 3180 SATA_SS, sizeof(uint32_t) * 3, &wdr->sata_baseioh); 3181 if (rv != 0) { 3182 aprint_error("%s:%d:%d: couldn't subregion SATA regs\n", 3183 device_xname(MVSATA_DEV2(mvport)), hc, port); 3184 return rv; 3185 } 3186 wdr->sata_iot = mvport->port_iot; 3187 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, 3188 SATA_SC, sizeof(uint32_t), &wdr->sata_control); 3189 if (rv != 0) { 3190 aprint_error("%s:%d:%d: couldn't subregion SControl\n", 3191 device_xname(MVSATA_DEV2(mvport)), hc, port); 3192 return rv; 3193 } 3194 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, 3195 SATA_SS, sizeof(uint32_t), &wdr->sata_status); 3196 if (rv != 0) { 3197 aprint_error("%s:%d:%d: couldn't subregion SStatus\n", 3198 device_xname(MVSATA_DEV2(mvport)), hc, port); 3199 return rv; 3200 } 3201 rv = bus_space_subregion(mvport->port_iot, mvport->port_ioh, 3202 SATA_SE, sizeof(uint32_t), &wdr->sata_error); 3203 if (rv != 0) { 3204 aprint_error("%s:%d:%d: couldn't subregion SError\n", 3205 device_xname(MVSATA_DEV2(mvport)), hc, port); 3206 return rv; 3207 } 3208 3209 return 0; 3210 } 3211 3212 3213 #ifndef MVSATA_WITHOUTDMA 3214 static void * 3215 mvsata_edma_resource_prepare(struct mvsata_port *mvport, bus_dma_tag_t dmat, 3216 bus_dmamap_t *dmamap, size_t size, int write) 3217 { 3218 bus_dma_segment_t seg; 3219 int nseg, rv; 3220 void *kva; 3221 3222 rv = bus_dmamem_alloc(dmat, size, PAGE_SIZE, 0, &seg, 1, &nseg, 3223 BUS_DMA_NOWAIT); 3224 if (rv != 0) { 3225 aprint_error("%s:%d:%d: DMA memory alloc failed: error=%d\n", 3226 device_xname(MVSATA_DEV2(mvport)), 3227 mvport->port_hc->hc, mvport->port, rv); 3228 goto fail; 3229 } 3230 3231 rv = bus_dmamem_map(dmat, &seg, nseg, size, &kva, BUS_DMA_NOWAIT); 3232 if (rv != 0) { 3233 aprint_error("%s:%d:%d: DMA memory map failed: error=%d\n", 3234 device_xname(MVSATA_DEV2(mvport)), 3235 mvport->port_hc->hc, mvport->port, rv); 3236 goto free; 3237 } 3238 3239 rv = bus_dmamap_load(dmat, *dmamap, kva, size, NULL, 3240 BUS_DMA_NOWAIT | (write ? BUS_DMA_WRITE : BUS_DMA_READ)); 3241 if (rv != 0) { 3242 aprint_error("%s:%d:%d: DMA map load failed: error=%d\n", 3243 device_xname(MVSATA_DEV2(mvport)), 3244 mvport->port_hc->hc, mvport->port, rv); 3245 goto unmap; 3246 } 3247 3248 if (!write) 3249 bus_dmamap_sync(dmat, *dmamap, 0, size, BUS_DMASYNC_PREREAD); 3250 3251 return kva; 3252 3253 unmap: 3254 bus_dmamem_unmap(dmat, kva, size); 3255 free: 3256 bus_dmamem_free(dmat, &seg, nseg); 3257 fail: 3258 return NULL; 3259 } 3260 3261 /* ARGSUSED */ 3262 static void 3263 mvsata_edma_resource_purge(struct mvsata_port *mvport, bus_dma_tag_t dmat, 3264 bus_dmamap_t dmamap, void *kva) 3265 { 3266 3267 bus_dmamap_unload(dmat, dmamap); 3268 bus_dmamem_unmap(dmat, kva, dmamap->dm_mapsize); 3269 bus_dmamem_free(dmat, dmamap->dm_segs, dmamap->dm_nsegs); 3270 } 3271 3272 static int 3273 mvsata_dma_bufload(struct mvsata_port *mvport, int index, void *databuf, 3274 size_t datalen, int flags) 3275 { 3276 int rv, lop, sop; 3277 bus_dmamap_t data_dmamap = mvport->port_reqtbl[index].data_dmamap; 3278 3279 lop = (flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE; 3280 sop = (flags & ATA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 3281 3282 rv = bus_dmamap_load(mvport->port_dmat, data_dmamap, databuf, datalen, 3283 NULL, BUS_DMA_NOWAIT | lop); 3284 if (rv) { 3285 aprint_error("%s:%d:%d: buffer load failed: error=%d", 3286 device_xname(MVSATA_DEV2(mvport)), mvport->port_hc->hc, 3287 mvport->port, rv); 3288 return rv; 3289 } 3290 bus_dmamap_sync(mvport->port_dmat, data_dmamap, 0, 3291 data_dmamap->dm_mapsize, sop); 3292 3293 return 0; 3294 } 3295 3296 static inline void 3297 mvsata_dma_bufunload(struct mvsata_port *mvport, int index, int flags) 3298 { 3299 bus_dmamap_t data_dmamap = mvport->port_reqtbl[index].data_dmamap; 3300 3301 bus_dmamap_sync(mvport->port_dmat, data_dmamap, 0, 3302 data_dmamap->dm_mapsize, 3303 (flags & ATA_READ) ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 3304 bus_dmamap_unload(mvport->port_dmat, data_dmamap); 3305 } 3306 #endif 3307 3308 static void 3309 mvsata_hreset_port(struct mvsata_port *mvport) 3310 { 3311 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 3312 3313 MVSATA_EDMA_WRITE_4(mvport, EDMA_CMD, EDMA_CMD_EATARST); 3314 3315 delay(25); /* allow reset propagation */ 3316 3317 MVSATA_EDMA_WRITE_4(mvport, EDMA_CMD, 0); 3318 3319 mvport->_fix_phy_param._fix_phy(mvport); 3320 3321 if (sc->sc_gen == gen1) 3322 delay(1000); 3323 } 3324 3325 static void 3326 mvsata_reset_port(struct mvsata_port *mvport) 3327 { 3328 device_t parent = device_parent(MVSATA_DEV2(mvport)); 3329 3330 MVSATA_EDMA_WRITE_4(mvport, EDMA_CMD, EDMA_CMD_EDSEDMA); 3331 3332 mvsata_hreset_port(mvport); 3333 3334 if (device_is_a(parent, "pci")) 3335 MVSATA_EDMA_WRITE_4(mvport, EDMA_CFG, 3336 EDMA_CFG_RESERVED | EDMA_CFG_ERDBSZ); 3337 else /* SoC */ 3338 MVSATA_EDMA_WRITE_4(mvport, EDMA_CFG, 3339 EDMA_CFG_RESERVED | EDMA_CFG_RESERVED2); 3340 MVSATA_EDMA_WRITE_4(mvport, EDMA_T, 0); 3341 MVSATA_EDMA_WRITE_4(mvport, SATA_SEIM, 0x019c0000); 3342 MVSATA_EDMA_WRITE_4(mvport, SATA_SE, ~0); 3343 MVSATA_EDMA_WRITE_4(mvport, SATA_FISIC, 0); 3344 MVSATA_EDMA_WRITE_4(mvport, EDMA_IEC, 0); 3345 MVSATA_EDMA_WRITE_4(mvport, EDMA_IEM, 0); 3346 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQBAH, 0); 3347 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQIP, 0); 3348 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQOP, 0); 3349 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQBAH, 0); 3350 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQIP, 0); 3351 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQOP, 0); 3352 MVSATA_EDMA_WRITE_4(mvport, EDMA_CMD, 0); 3353 MVSATA_EDMA_WRITE_4(mvport, EDMA_TC, 0); 3354 MVSATA_EDMA_WRITE_4(mvport, EDMA_IORT, 0xbc); 3355 } 3356 3357 static void 3358 mvsata_reset_hc(struct mvsata_hc *mvhc) 3359 { 3360 #if 0 3361 uint32_t val; 3362 #endif 3363 3364 MVSATA_HC_WRITE_4(mvhc, SATAHC_ICT, 0); 3365 MVSATA_HC_WRITE_4(mvhc, SATAHC_ITT, 0); 3366 MVSATA_HC_WRITE_4(mvhc, SATAHC_IC, 0); 3367 3368 #if 0 /* XXXX needs? */ 3369 MVSATA_HC_WRITE_4(mvhc, 0x01c, 0); 3370 3371 /* 3372 * Keep the SS during power on and the reference clock bits (reset 3373 * sample) 3374 */ 3375 val = MVSATA_HC_READ_4(mvhc, 0x020); 3376 val &= 0x1c1c1c1c; 3377 val |= 0x03030303; 3378 MVSATA_HC_READ_4(mvhc, 0x020, 0); 3379 #endif 3380 } 3381 3382 static uint32_t 3383 mvsata_softreset(struct mvsata_port *mvport, int flags) 3384 { 3385 struct ata_channel *chp = &mvport->port_ata_channel; 3386 uint32_t sig0 = ~0; 3387 int timeout; 3388 uint8_t st0; 3389 3390 ata_channel_lock_owned(chp); 3391 3392 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_RST | WDCTL_IDS | WDCTL_4BIT); 3393 delay(10); 3394 (void) MVSATA_WDC_READ_1(mvport, SRB_FE); 3395 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_IDS | WDCTL_4BIT); 3396 delay(10); 3397 3398 /* wait for BSY to deassert */ 3399 for (timeout = 0; timeout < WDC_RESET_WAIT / 10; timeout++) { 3400 st0 = MVSATA_WDC_READ_1(mvport, SRB_CS); 3401 3402 if ((st0 & WDCS_BSY) == 0) { 3403 sig0 = MVSATA_WDC_READ_1(mvport, SRB_SC) << 0; 3404 sig0 |= MVSATA_WDC_READ_1(mvport, SRB_LBAL) << 8; 3405 sig0 |= MVSATA_WDC_READ_1(mvport, SRB_LBAM) << 16; 3406 sig0 |= MVSATA_WDC_READ_1(mvport, SRB_LBAH) << 24; 3407 goto out; 3408 } 3409 ata_delay(chp, 10, "atarst", flags); 3410 } 3411 3412 aprint_error("%s:%d:%d: %s: timeout\n", 3413 device_xname(MVSATA_DEV2(mvport)), 3414 mvport->port_hc->hc, mvport->port, __func__); 3415 3416 out: 3417 MVSATA_WDC_WRITE_1(mvport, SRB_CAS, WDCTL_4BIT); 3418 return sig0; 3419 } 3420 3421 #ifndef MVSATA_WITHOUTDMA 3422 static void 3423 mvsata_edma_reset_qptr(struct mvsata_port *mvport) 3424 { 3425 const bus_addr_t crpb_addr = 3426 mvport->port_crpb_dmamap->dm_segs[0].ds_addr; 3427 const uint32_t crpb_addr_mask = 3428 EDMA_RESQP_ERPQBAP_MASK | EDMA_RESQP_ERPQBA_MASK; 3429 3430 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQBAH, 0); 3431 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQIP, 0); 3432 MVSATA_EDMA_WRITE_4(mvport, EDMA_REQQOP, 0); 3433 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQBAH, (crpb_addr >> 16) >> 16); 3434 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQIP, 0); 3435 MVSATA_EDMA_WRITE_4(mvport, EDMA_RESQOP, (crpb_addr & crpb_addr_mask)); 3436 } 3437 3438 static inline void 3439 mvsata_edma_enable(struct mvsata_port *mvport) 3440 { 3441 3442 MVSATA_EDMA_WRITE_4(mvport, EDMA_CMD, EDMA_CMD_EENEDMA); 3443 } 3444 3445 static int 3446 mvsata_edma_disable(struct mvsata_port *mvport, int timeout, int wflags) 3447 { 3448 struct ata_channel *chp = &mvport->port_ata_channel; 3449 uint32_t status, command; 3450 uint32_t idlestatus = EDMA_S_EDMAIDLE | EDMA_S_ECACHEEMPTY; 3451 int t; 3452 3453 ata_channel_lock_owned(chp); 3454 3455 if (MVSATA_EDMA_READ_4(mvport, EDMA_CMD) & EDMA_CMD_EENEDMA) { 3456 3457 timeout = mstohz(timeout + hztoms(1) - 1); 3458 3459 for (t = 0; ; ++t) { 3460 status = MVSATA_EDMA_READ_4(mvport, EDMA_S); 3461 if ((status & idlestatus) == idlestatus) 3462 break; 3463 if (t >= timeout) 3464 break; 3465 ata_delay(chp, hztoms(1), "mvsata_edma1", wflags); 3466 } 3467 if (t >= timeout) { 3468 aprint_error("%s:%d:%d: unable to stop EDMA\n", 3469 device_xname(MVSATA_DEV2(mvport)), 3470 mvport->port_hc->hc, mvport->port); 3471 return EBUSY; 3472 } 3473 3474 /* The disable bit (eDsEDMA) is self negated. */ 3475 MVSATA_EDMA_WRITE_4(mvport, EDMA_CMD, EDMA_CMD_EDSEDMA); 3476 3477 for (t = 0; ; ++t) { 3478 command = MVSATA_EDMA_READ_4(mvport, EDMA_CMD); 3479 if (!(command & EDMA_CMD_EENEDMA)) 3480 break; 3481 if (t >= timeout) 3482 break; 3483 ata_delay(chp, hztoms(1), "mvsata_edma2", wflags); 3484 } 3485 if (t >= timeout) { 3486 aprint_error("%s:%d:%d: unable to re-enable EDMA\n", 3487 device_xname(MVSATA_DEV2(mvport)), 3488 mvport->port_hc->hc, mvport->port); 3489 return EBUSY; 3490 } 3491 } 3492 return 0; 3493 } 3494 3495 /* 3496 * Set EDMA registers according to mode. 3497 * ex. NCQ/TCQ(queued)/non queued. 3498 */ 3499 static void 3500 mvsata_edma_config(struct mvsata_port *mvport, enum mvsata_edmamode mode) 3501 { 3502 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 3503 uint32_t reg; 3504 3505 reg = MVSATA_EDMA_READ_4(mvport, EDMA_CFG); 3506 reg |= EDMA_CFG_RESERVED; 3507 3508 if (mode == ncq) { 3509 if (sc->sc_gen == gen1) { 3510 aprint_error_dev(MVSATA_DEV2(mvport), 3511 "GenI not support NCQ\n"); 3512 return; 3513 } else if (sc->sc_gen == gen2) 3514 reg |= EDMA_CFG_EDEVERR; 3515 reg |= EDMA_CFG_ESATANATVCMDQUE; 3516 } else if (mode == queued) { 3517 reg &= ~EDMA_CFG_ESATANATVCMDQUE; 3518 reg |= EDMA_CFG_EQUE; 3519 } else 3520 reg &= ~(EDMA_CFG_ESATANATVCMDQUE | EDMA_CFG_EQUE); 3521 3522 if (sc->sc_gen == gen1) 3523 reg |= EDMA_CFG_ERDBSZ; 3524 else if (sc->sc_gen == gen2) 3525 reg |= (EDMA_CFG_ERDBSZEXT | EDMA_CFG_EWRBUFFERLEN); 3526 else if (sc->sc_gen == gen2e) { 3527 device_t parent = device_parent(MVSATA_DEV(sc)); 3528 3529 reg |= (EDMA_CFG_EMASKRXPM | EDMA_CFG_EHOSTQUEUECACHEEN); 3530 reg &= ~(EDMA_CFG_EEDMAFBS | EDMA_CFG_EEDMAQUELEN); 3531 3532 if (device_is_a(parent, "pci")) 3533 reg |= ( 3534 #if NATAPIBUS > 0 3535 EDMA_CFG_EEARLYCOMPLETIONEN | 3536 #endif 3537 EDMA_CFG_ECUTTHROUGHEN | 3538 EDMA_CFG_EWRBUFFERLEN | 3539 EDMA_CFG_ERDBSZEXT); 3540 } 3541 MVSATA_EDMA_WRITE_4(mvport, EDMA_CFG, reg); 3542 3543 reg = ( 3544 EDMA_IE_EIORDYERR | 3545 EDMA_IE_ETRANSINT | 3546 EDMA_IE_EDEVCON | 3547 EDMA_IE_EDEVDIS); 3548 if (sc->sc_gen != gen1) 3549 reg |= ( 3550 EDMA_IE_TRANSPROTERR | 3551 EDMA_IE_LINKDATATXERR(EDMA_IE_LINKTXERR_FISTXABORTED) | 3552 EDMA_IE_LINKDATATXERR(EDMA_IE_LINKXERR_OTHERERRORS) | 3553 EDMA_IE_LINKDATATXERR(EDMA_IE_LINKXERR_LINKLAYERRESET) | 3554 EDMA_IE_LINKDATATXERR(EDMA_IE_LINKXERR_INTERNALFIFO) | 3555 EDMA_IE_LINKDATATXERR(EDMA_IE_LINKXERR_SATACRC) | 3556 EDMA_IE_LINKCTLTXERR(EDMA_IE_LINKXERR_OTHERERRORS) | 3557 EDMA_IE_LINKCTLTXERR(EDMA_IE_LINKXERR_LINKLAYERRESET) | 3558 EDMA_IE_LINKCTLTXERR(EDMA_IE_LINKXERR_INTERNALFIFO) | 3559 EDMA_IE_LINKDATARXERR(EDMA_IE_LINKXERR_OTHERERRORS) | 3560 EDMA_IE_LINKDATARXERR(EDMA_IE_LINKXERR_LINKLAYERRESET) | 3561 EDMA_IE_LINKDATARXERR(EDMA_IE_LINKXERR_INTERNALFIFO) | 3562 EDMA_IE_LINKDATARXERR(EDMA_IE_LINKXERR_SATACRC) | 3563 EDMA_IE_LINKCTLRXERR(EDMA_IE_LINKXERR_OTHERERRORS) | 3564 EDMA_IE_LINKCTLRXERR(EDMA_IE_LINKXERR_LINKLAYERRESET) | 3565 EDMA_IE_LINKCTLRXERR(EDMA_IE_LINKXERR_INTERNALFIFO) | 3566 EDMA_IE_LINKCTLRXERR(EDMA_IE_LINKXERR_SATACRC) | 3567 EDMA_IE_ESELFDIS); 3568 3569 if (mode == ncq) 3570 reg |= EDMA_IE_EDEVERR; 3571 MVSATA_EDMA_WRITE_4(mvport, EDMA_IEM, reg); 3572 reg = MVSATA_EDMA_READ_4(mvport, EDMA_HC); 3573 reg &= ~EDMA_IE_EDEVERR; 3574 if (mode != ncq) 3575 reg |= EDMA_IE_EDEVERR; 3576 MVSATA_EDMA_WRITE_4(mvport, EDMA_HC, reg); 3577 if (sc->sc_gen == gen2e) { 3578 /* 3579 * Clear FISWait4HostRdyEn[0] and [2]. 3580 * [0]: Device to Host FIS with <ERR> or <DF> bit set to 1. 3581 * [2]: SDB FIS is received with <ERR> bit set to 1. 3582 */ 3583 reg = MVSATA_EDMA_READ_4(mvport, SATA_FISC); 3584 reg &= ~(SATA_FISC_FISWAIT4HOSTRDYEN_B0 | 3585 SATA_FISC_FISWAIT4HOSTRDYEN_B2); 3586 MVSATA_EDMA_WRITE_4(mvport, SATA_FISC, reg); 3587 } 3588 3589 mvport->port_edmamode_curr = mode; 3590 } 3591 3592 3593 /* 3594 * Generation dependent functions 3595 */ 3596 3597 static void 3598 mvsata_edma_setup_crqb(struct mvsata_port *mvport, int erqqip, 3599 struct ata_xfer *xfer) 3600 { 3601 struct crqb *crqb; 3602 bus_addr_t eprd_addr; 3603 daddr_t blkno; 3604 uint32_t rw; 3605 uint8_t cmd, head; 3606 int i; 3607 struct ata_bio *ata_bio = &xfer->c_bio; 3608 3609 eprd_addr = mvport->port_eprd_dmamap->dm_segs[0].ds_addr + 3610 mvport->port_reqtbl[xfer->c_slot].eprd_offset; 3611 rw = (ata_bio->flags & ATA_READ) ? CRQB_CDIR_READ : CRQB_CDIR_WRITE; 3612 cmd = (ata_bio->flags & ATA_READ) ? WDCC_READDMA : WDCC_WRITEDMA; 3613 if (ata_bio->flags & (ATA_LBA|ATA_LBA48)) { 3614 head = WDSD_LBA; 3615 } else { 3616 head = 0; 3617 } 3618 blkno = ata_bio->blkno; 3619 if (ata_bio->flags & ATA_LBA48) 3620 cmd = atacmd_to48(cmd); 3621 else { 3622 head |= ((ata_bio->blkno >> 24) & 0xf); 3623 blkno &= 0xffffff; 3624 } 3625 crqb = &mvport->port_crqb->crqb + erqqip; 3626 crqb->cprdbl = htole32(eprd_addr & CRQB_CRQBL_EPRD_MASK); 3627 crqb->cprdbh = htole32((eprd_addr >> 16) >> 16); 3628 crqb->ctrlflg = 3629 htole16(rw | CRQB_CHOSTQUETAG(xfer->c_slot) | 3630 CRQB_CPMPORT(xfer->c_drive)); 3631 i = 0; 3632 if (mvport->port_edmamode_curr == dma) { 3633 if (ata_bio->flags & ATA_LBA48) 3634 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3635 CRQB_ATACOMMAND_SECTORCOUNT, ata_bio->nblks >> 8)); 3636 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3637 CRQB_ATACOMMAND_SECTORCOUNT, ata_bio->nblks)); 3638 } else { /* ncq/queued */ 3639 3640 /* 3641 * XXXX: Oops, ata command is not correct. And, atabus layer 3642 * has not been supported yet now. 3643 * Queued DMA read/write. 3644 * read/write FPDMAQueued. 3645 */ 3646 3647 if (ata_bio->flags & ATA_LBA48) 3648 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3649 CRQB_ATACOMMAND_FEATURES, ata_bio->nblks >> 8)); 3650 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3651 CRQB_ATACOMMAND_FEATURES, ata_bio->nblks)); 3652 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3653 CRQB_ATACOMMAND_SECTORCOUNT, xfer->c_slot << 3)); 3654 } 3655 if (ata_bio->flags & ATA_LBA48) { 3656 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3657 CRQB_ATACOMMAND_LBALOW, blkno >> 24)); 3658 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3659 CRQB_ATACOMMAND_LBAMID, blkno >> 32)); 3660 crqb->atacommand[i++] = htole16(CRQB_ATACOMMAND( 3661 CRQB_ATACOMMAND_LBAHIGH, blkno >> 40)); 3662 } 3663 crqb->atacommand[i++] = 3664 htole16(CRQB_ATACOMMAND(CRQB_ATACOMMAND_LBALOW, blkno)); 3665 crqb->atacommand[i++] = 3666 htole16(CRQB_ATACOMMAND(CRQB_ATACOMMAND_LBAMID, blkno >> 8)); 3667 crqb->atacommand[i++] = 3668 htole16(CRQB_ATACOMMAND(CRQB_ATACOMMAND_LBAHIGH, blkno >> 16)); 3669 crqb->atacommand[i++] = 3670 htole16(CRQB_ATACOMMAND(CRQB_ATACOMMAND_DEVICE, head)); 3671 crqb->atacommand[i++] = htole16( 3672 CRQB_ATACOMMAND(CRQB_ATACOMMAND_COMMAND, cmd) | 3673 CRQB_ATACOMMAND_LAST); 3674 } 3675 #endif 3676 3677 static uint32_t 3678 mvsata_read_preamps_gen1(struct mvsata_port *mvport) 3679 { 3680 struct mvsata_hc *hc = mvport->port_hc; 3681 uint32_t reg; 3682 3683 reg = MVSATA_HC_READ_4(hc, SATAHC_I_PHYMODE(mvport->port)); 3684 /* 3685 * [12:11] : pre 3686 * [7:5] : amps 3687 */ 3688 return reg & 0x000018e0; 3689 } 3690 3691 static void 3692 mvsata_fix_phy_gen1(struct mvsata_port *mvport) 3693 { 3694 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 3695 struct mvsata_hc *mvhc = mvport->port_hc; 3696 uint32_t reg; 3697 int port = mvport->port, fix_apm_sq = 0; 3698 3699 if (sc->sc_model == PCI_PRODUCT_MARVELL_88SX5080) { 3700 if (sc->sc_rev == 0x01) 3701 fix_apm_sq = 1; 3702 } else { 3703 if (sc->sc_rev == 0x00) 3704 fix_apm_sq = 1; 3705 } 3706 3707 if (fix_apm_sq) { 3708 /* 3709 * Disable auto-power management 3710 * 88SX50xx FEr SATA#12 3711 */ 3712 reg = MVSATA_HC_READ_4(mvhc, SATAHC_I_LTMODE(port)); 3713 reg |= (1 << 19); 3714 MVSATA_HC_WRITE_4(mvhc, SATAHC_I_LTMODE(port), reg); 3715 3716 /* 3717 * Fix squelch threshold 3718 * 88SX50xx FEr SATA#9 3719 */ 3720 reg = MVSATA_HC_READ_4(mvhc, SATAHC_I_PHYCONTROL(port)); 3721 reg &= ~0x3; 3722 reg |= 0x1; 3723 MVSATA_HC_WRITE_4(mvhc, SATAHC_I_PHYCONTROL(port), reg); 3724 } 3725 3726 /* Revert values of pre-emphasis and signal amps to the saved ones */ 3727 reg = MVSATA_HC_READ_4(mvhc, SATAHC_I_PHYMODE(port)); 3728 reg &= ~0x000018e0; /* pre and amps mask */ 3729 reg |= mvport->_fix_phy_param.pre_amps; 3730 MVSATA_HC_WRITE_4(mvhc, SATAHC_I_PHYMODE(port), reg); 3731 } 3732 3733 static void 3734 mvsata_devconn_gen1(struct mvsata_port *mvport) 3735 { 3736 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 3737 3738 /* Fix for 88SX50xx FEr SATA#2 */ 3739 mvport->_fix_phy_param._fix_phy(mvport); 3740 3741 /* If disk is connected, then enable the activity LED */ 3742 if (sc->sc_rev == 0x03) { 3743 /* XXXXX */ 3744 } 3745 } 3746 3747 static uint32_t 3748 mvsata_read_preamps_gen2(struct mvsata_port *mvport) 3749 { 3750 uint32_t reg; 3751 3752 reg = MVSATA_EDMA_READ_4(mvport, SATA_PHYM2); 3753 /* 3754 * [10:8] : amps 3755 * [7:5] : pre 3756 */ 3757 return reg & 0x000007e0; 3758 } 3759 3760 static void 3761 mvsata_fix_phy_gen2(struct mvsata_port *mvport) 3762 { 3763 struct mvsata_softc *sc = device_private(MVSATA_DEV2(mvport)); 3764 uint32_t reg; 3765 3766 if ((sc->sc_gen == gen2 && sc->sc_rev == 0x07) || 3767 sc->sc_gen == gen2e) { 3768 /* 3769 * Fix for 3770 * 88SX60X1 FEr SATA #23 3771 * 88SX6042/88SX7042 FEr SATA #23 3772 * 88F5182 FEr #SATA-S13 3773 * 88F5082 FEr #SATA-S13 3774 */ 3775 reg = MVSATA_EDMA_READ_4(mvport, SATA_PHYM2); 3776 reg &= ~(1 << 16); 3777 reg |= (1 << 31); 3778 MVSATA_EDMA_WRITE_4(mvport, SATA_PHYM2, reg); 3779 3780 delay(200); 3781 3782 reg = MVSATA_EDMA_READ_4(mvport, SATA_PHYM2); 3783 reg &= ~((1 << 16) | (1 << 31)); 3784 MVSATA_EDMA_WRITE_4(mvport, SATA_PHYM2, reg); 3785 3786 delay(200); 3787 } 3788 3789 /* Fix values in PHY Mode 3 Register.*/ 3790 reg = MVSATA_EDMA_READ_4(mvport, SATA_PHYM3); 3791 reg &= ~0x7F900000; 3792 reg |= 0x2A800000; 3793 /* Implement Guidline 88F5182, 88F5082, 88F6082 (GL# SATA-S11) */ 3794 if (sc->sc_model == PCI_PRODUCT_MARVELL_88F5082 || 3795 sc->sc_model == PCI_PRODUCT_MARVELL_88F5182 || 3796 sc->sc_model == PCI_PRODUCT_MARVELL_88F6082) 3797 reg &= ~0x0000001c; 3798 MVSATA_EDMA_WRITE_4(mvport, SATA_PHYM3, reg); 3799 3800 /* 3801 * Fix values in PHY Mode 4 Register. 3802 * 88SX60x1 FEr SATA#10 3803 * 88F5182 GL #SATA-S10 3804 * 88F5082 GL #SATA-S10 3805 */ 3806 if ((sc->sc_gen == gen2 && sc->sc_rev == 0x07) || 3807 sc->sc_gen == gen2e) { 3808 uint32_t tmp = 0; 3809 3810 /* 88SX60x1 FEr SATA #13 */ 3811 if (sc->sc_gen == 2 && sc->sc_rev == 0x07) 3812 tmp = MVSATA_EDMA_READ_4(mvport, SATA_PHYM3); 3813 3814 reg = MVSATA_EDMA_READ_4(mvport, SATA_PHYM4); 3815 reg |= (1 << 0); 3816 reg &= ~(1 << 1); 3817 /* PHY Mode 4 Register of Gen IIE has some restriction */ 3818 if (sc->sc_gen == gen2e) { 3819 reg &= ~0x5de3fffc; 3820 reg |= (1 << 2); 3821 } 3822 MVSATA_EDMA_WRITE_4(mvport, SATA_PHYM4, reg); 3823 3824 /* 88SX60x1 FEr SATA #13 */ 3825 if (sc->sc_gen == 2 && sc->sc_rev == 0x07) 3826 MVSATA_EDMA_WRITE_4(mvport, SATA_PHYM3, tmp); 3827 } 3828 3829 /* Revert values of pre-emphasis and signal amps to the saved ones */ 3830 reg = MVSATA_EDMA_READ_4(mvport, SATA_PHYM2); 3831 reg &= ~0x000007e0; /* pre and amps mask */ 3832 reg |= mvport->_fix_phy_param.pre_amps; 3833 reg &= ~(1 << 16); 3834 if (sc->sc_gen == gen2e) { 3835 /* 3836 * according to mvSata 3.6.1, some IIE values are fixed. 3837 * some reserved fields must be written with fixed values. 3838 */ 3839 reg &= ~0xC30FF01F; 3840 reg |= 0x0000900F; 3841 } 3842 MVSATA_EDMA_WRITE_4(mvport, SATA_PHYM2, reg); 3843 } 3844 3845 #ifndef MVSATA_WITHOUTDMA 3846 static void 3847 mvsata_edma_setup_crqb_gen2e(struct mvsata_port *mvport, int erqqip, 3848 struct ata_xfer *xfer) 3849 { 3850 struct crqb_gen2e *crqb; 3851 bus_addr_t eprd_addr; 3852 uint32_t ctrlflg, rw; 3853 uint8_t fis[RHD_FISLEN]; 3854 3855 eprd_addr = mvport->port_eprd_dmamap->dm_segs[0].ds_addr + 3856 mvport->port_reqtbl[xfer->c_slot].eprd_offset; 3857 rw = (xfer->c_bio.flags & ATA_READ) ? CRQB_CDIR_READ : CRQB_CDIR_WRITE; 3858 ctrlflg = (rw | CRQB_CDEVICEQUETAG(xfer->c_slot) | 3859 CRQB_CPMPORT(xfer->c_drive) | 3860 CRQB_CPRDMODE_EPRD | CRQB_CHOSTQUETAG_GEN2(xfer->c_slot)); 3861 3862 crqb = &mvport->port_crqb->crqb_gen2e + erqqip; 3863 crqb->cprdbl = htole32(eprd_addr & CRQB_CRQBL_EPRD_MASK); 3864 crqb->cprdbh = htole32((eprd_addr >> 16) >> 16); 3865 crqb->ctrlflg = htole32(ctrlflg); 3866 3867 satafis_rhd_construct_bio(xfer, fis); 3868 3869 crqb->atacommand[0] = 0; 3870 crqb->atacommand[1] = 0; 3871 /* copy over the ATA command part of the fis */ 3872 memcpy(&crqb->atacommand[2], &fis[rhd_command], 3873 MIN(sizeof(crqb->atacommand) - 2, RHD_FISLEN - rhd_command)); 3874 } 3875 3876 #ifdef MVSATA_DEBUG 3877 #define MVSATA_DEBUG_PRINT(type, size, n, p) \ 3878 do { \ 3879 int _i; \ 3880 u_char *_p = (p); \ 3881 \ 3882 printf(#type "(%d)", (n)); \ 3883 for (_i = 0; _i < (size); _i++, _p++) { \ 3884 if (_i % 16 == 0) \ 3885 printf("\n "); \ 3886 printf(" %02x", *_p); \ 3887 } \ 3888 printf("\n"); \ 3889 } while (0 /* CONSTCOND */) 3890 3891 static void 3892 mvsata_print_crqb(struct mvsata_port *mvport, int n) 3893 { 3894 3895 MVSATA_DEBUG_PRINT(crqb, sizeof(union mvsata_crqb), 3896 n, (u_char *)(mvport->port_crqb + n)); 3897 } 3898 3899 static void 3900 mvsata_print_crpb(struct mvsata_port *mvport, int n) 3901 { 3902 3903 MVSATA_DEBUG_PRINT(crpb, sizeof(struct crpb), 3904 n, (u_char *)(mvport->port_crpb + n)); 3905 } 3906 3907 static void 3908 mvsata_print_eprd(struct mvsata_port *mvport, int n) 3909 { 3910 struct eprd *eprd; 3911 int i = 0; 3912 3913 eprd = mvport->port_reqtbl[n].eprd; 3914 while (1 /*CONSTCOND*/) { 3915 MVSATA_DEBUG_PRINT(eprd, sizeof(struct eprd), 3916 i, (u_char *)eprd); 3917 if (eprd->eot & EPRD_EOT) 3918 break; 3919 eprd++; 3920 i++; 3921 } 3922 } 3923 #endif 3924 #endif 3925