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