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