xref: /freebsd-src/sys/dev/mvs/mvs_pci.c (revision 3ddaf8200bc90b1410755ebac7b5c979ea90a2f6)
1dd48af36SAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4dd48af36SAlexander Motin  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5dd48af36SAlexander Motin  * All rights reserved.
6dd48af36SAlexander Motin  *
7dd48af36SAlexander Motin  * Redistribution and use in source and binary forms, with or without
8dd48af36SAlexander Motin  * modification, are permitted provided that the following conditions
9dd48af36SAlexander Motin  * are met:
10dd48af36SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
11dd48af36SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
12dd48af36SAlexander Motin  *    without modification, immediately at the beginning of the file.
13dd48af36SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14dd48af36SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15dd48af36SAlexander Motin  *    documentation and/or other materials provided with the distribution.
16dd48af36SAlexander Motin  *
17dd48af36SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18dd48af36SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19dd48af36SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20dd48af36SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21dd48af36SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22dd48af36SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23dd48af36SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24dd48af36SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25dd48af36SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26dd48af36SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27dd48af36SAlexander Motin  */
28dd48af36SAlexander Motin 
29dd48af36SAlexander Motin #include <sys/param.h>
30dd48af36SAlexander Motin #include <sys/module.h>
31dd48af36SAlexander Motin #include <sys/systm.h>
32dd48af36SAlexander Motin #include <sys/kernel.h>
33dd48af36SAlexander Motin #include <sys/bus.h>
34dd48af36SAlexander Motin #include <sys/endian.h>
35dd48af36SAlexander Motin #include <sys/malloc.h>
36dd48af36SAlexander Motin #include <sys/lock.h>
37dd48af36SAlexander Motin #include <sys/mutex.h>
38ddfc9c4cSWarner Losh #include <sys/sbuf.h>
39dd48af36SAlexander Motin #include <vm/uma.h>
40dd48af36SAlexander Motin #include <machine/stdarg.h>
41dd48af36SAlexander Motin #include <machine/resource.h>
42dd48af36SAlexander Motin #include <machine/bus.h>
43dd48af36SAlexander Motin #include <sys/rman.h>
44dd48af36SAlexander Motin #include <dev/pci/pcivar.h>
45dd48af36SAlexander Motin #include <dev/pci/pcireg.h>
46dd48af36SAlexander Motin #include "mvs.h"
47dd48af36SAlexander Motin 
48dd48af36SAlexander Motin /* local prototypes */
49dd48af36SAlexander Motin static int mvs_setup_interrupt(device_t dev);
50dd48af36SAlexander Motin static void mvs_intr(void *data);
51dd48af36SAlexander Motin static int mvs_suspend(device_t dev);
52dd48af36SAlexander Motin static int mvs_resume(device_t dev);
53dd48af36SAlexander Motin static int mvs_ctlr_setup(device_t dev);
54dd48af36SAlexander Motin 
55dd48af36SAlexander Motin static struct {
56dd48af36SAlexander Motin 	uint32_t	id;
57dd48af36SAlexander Motin 	uint8_t		rev;
58dd48af36SAlexander Motin 	const char	*name;
59dd48af36SAlexander Motin 	int		ports;
60dd48af36SAlexander Motin 	int		quirks;
61dd48af36SAlexander Motin } mvs_ids[] = {
62dd48af36SAlexander Motin 	{0x504011ab, 0x00, "Marvell 88SX5040",	4,	MVS_Q_GENI},
63dd48af36SAlexander Motin 	{0x504111ab, 0x00, "Marvell 88SX5041",	4,	MVS_Q_GENI},
64dd48af36SAlexander Motin 	{0x508011ab, 0x00, "Marvell 88SX5080",	8,	MVS_Q_GENI},
65dd48af36SAlexander Motin 	{0x508111ab, 0x00, "Marvell 88SX5081",	8,	MVS_Q_GENI},
66dd48af36SAlexander Motin 	{0x604011ab, 0x00, "Marvell 88SX6040",	4,	MVS_Q_GENII},
67dd48af36SAlexander Motin 	{0x604111ab, 0x00, "Marvell 88SX6041",	4,	MVS_Q_GENII},
68dd48af36SAlexander Motin 	{0x604211ab, 0x00, "Marvell 88SX6042",	4,	MVS_Q_GENIIE},
69dd48af36SAlexander Motin 	{0x608011ab, 0x00, "Marvell 88SX6080",	8,	MVS_Q_GENII},
70dd48af36SAlexander Motin 	{0x608111ab, 0x00, "Marvell 88SX6081",	8,	MVS_Q_GENII},
71dd48af36SAlexander Motin 	{0x704211ab, 0x00, "Marvell 88SX7042",	4,	MVS_Q_GENIIE|MVS_Q_CT},
72dd48af36SAlexander Motin 	{0x02419005, 0x00, "Adaptec 1420SA",	4,	MVS_Q_GENII},
73dd48af36SAlexander Motin 	{0x02439005, 0x00, "Adaptec 1430SA",	4,	MVS_Q_GENIIE|MVS_Q_CT},
74dd48af36SAlexander Motin 	{0x00000000, 0x00, NULL,	0,	0}
75dd48af36SAlexander Motin };
76dd48af36SAlexander Motin 
77dd48af36SAlexander Motin static int
78dd48af36SAlexander Motin mvs_probe(device_t dev)
79dd48af36SAlexander Motin {
80dd48af36SAlexander Motin 	int i;
81dd48af36SAlexander Motin 	uint32_t devid = pci_get_devid(dev);
82dd48af36SAlexander Motin 	uint8_t revid = pci_get_revid(dev);
83dd48af36SAlexander Motin 
84dd48af36SAlexander Motin 	for (i = 0; mvs_ids[i].id != 0; i++) {
85dd48af36SAlexander Motin 		if (mvs_ids[i].id == devid &&
86dd48af36SAlexander Motin 		    mvs_ids[i].rev <= revid) {
87a3b460d4SMark Johnston 			device_set_descf(dev, "%s SATA controller",
88dd48af36SAlexander Motin 			    mvs_ids[i].name);
893036de3cSAlexander Motin 			return (BUS_PROBE_DEFAULT);
90dd48af36SAlexander Motin 		}
91dd48af36SAlexander Motin 	}
92dd48af36SAlexander Motin 	return (ENXIO);
93dd48af36SAlexander Motin }
94dd48af36SAlexander Motin 
95dd48af36SAlexander Motin static int
96dd48af36SAlexander Motin mvs_attach(device_t dev)
97dd48af36SAlexander Motin {
98dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
99dd48af36SAlexander Motin 	device_t child;
100dd48af36SAlexander Motin 	int	error, unit, i;
101dd48af36SAlexander Motin 	uint32_t devid = pci_get_devid(dev);
102dd48af36SAlexander Motin 	uint8_t revid = pci_get_revid(dev);
103dd48af36SAlexander Motin 
104dd48af36SAlexander Motin 	ctlr->dev = dev;
105dd48af36SAlexander Motin 	i = 0;
106dd48af36SAlexander Motin 	while (mvs_ids[i].id != 0 &&
107dd48af36SAlexander Motin 	    (mvs_ids[i].id != devid ||
108dd48af36SAlexander Motin 	     mvs_ids[i].rev > revid))
109dd48af36SAlexander Motin 		i++;
110dd48af36SAlexander Motin 	ctlr->channels = mvs_ids[i].ports;
111dd48af36SAlexander Motin 	ctlr->quirks = mvs_ids[i].quirks;
112200b4021SAlexander Motin 	ctlr->ccc = 0;
113dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
114dd48af36SAlexander Motin 	    device_get_unit(dev), "ccc", &ctlr->ccc);
115dd48af36SAlexander Motin 	ctlr->cccc = 8;
116dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
117dd48af36SAlexander Motin 	    device_get_unit(dev), "cccc", &ctlr->cccc);
118dd48af36SAlexander Motin 	if (ctlr->ccc == 0 || ctlr->cccc == 0) {
119dd48af36SAlexander Motin 		ctlr->ccc = 0;
120dd48af36SAlexander Motin 		ctlr->cccc = 0;
121dd48af36SAlexander Motin 	}
122dd48af36SAlexander Motin 	if (ctlr->ccc > 100000)
123dd48af36SAlexander Motin 		ctlr->ccc = 100000;
124dd48af36SAlexander Motin 	device_printf(dev,
125dd48af36SAlexander Motin 	    "Gen-%s, %d %sGbps ports, Port Multiplier %s%s\n",
126dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "I" :
127dd48af36SAlexander Motin 	     ((ctlr->quirks & MVS_Q_GENII) ? "II" : "IIe")),
128dd48af36SAlexander Motin 	    ctlr->channels,
129dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "1.5" : "3"),
130dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ?
131dd48af36SAlexander Motin 	    "not supported" : "supported"),
132dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENIIE) ?
133dd48af36SAlexander Motin 	    " with FBS" : ""));
134dd48af36SAlexander Motin 	mtx_init(&ctlr->mtx, "MVS controller lock", NULL, MTX_DEF);
135dd48af36SAlexander Motin 	/* We should have a memory BAR(0). */
136dd48af36SAlexander Motin 	ctlr->r_rid = PCIR_BAR(0);
137dd48af36SAlexander Motin 	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
138dd48af36SAlexander Motin 	    &ctlr->r_rid, RF_ACTIVE)))
139dd48af36SAlexander Motin 		return ENXIO;
140dd48af36SAlexander Motin 	/* Setup our own memory management for channels. */
141cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
142cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
143dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
144dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
145dd48af36SAlexander Motin 	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
146dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
147dd48af36SAlexander Motin 		return (error);
148dd48af36SAlexander Motin 	}
149dd48af36SAlexander Motin 	if ((error = rman_manage_region(&ctlr->sc_iomem,
150dd48af36SAlexander Motin 	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
151dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
152dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
153dd48af36SAlexander Motin 		return (error);
154dd48af36SAlexander Motin 	}
155dd48af36SAlexander Motin 	pci_enable_busmaster(dev);
156dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
157dd48af36SAlexander Motin 	/* Setup interrupts. */
158dd48af36SAlexander Motin 	if (mvs_setup_interrupt(dev)) {
159dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
160dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
161dd48af36SAlexander Motin 		return ENXIO;
162dd48af36SAlexander Motin 	}
163dd48af36SAlexander Motin 	/* Attach all channels on this controller */
164dd48af36SAlexander Motin 	for (unit = 0; unit < ctlr->channels; unit++) {
1655b56413dSWarner Losh 		child = device_add_child(dev, "mvsch", DEVICE_UNIT_ANY);
166dd48af36SAlexander Motin 		if (child == NULL)
167dd48af36SAlexander Motin 			device_printf(dev, "failed to add channel device\n");
168dd48af36SAlexander Motin 		else
169dd48af36SAlexander Motin 			device_set_ivars(child, (void *)(intptr_t)unit);
170dd48af36SAlexander Motin 	}
17118250ec6SJohn Baldwin 	bus_attach_children(dev);
172dd48af36SAlexander Motin 	return 0;
173dd48af36SAlexander Motin }
174dd48af36SAlexander Motin 
175dd48af36SAlexander Motin static int
176dd48af36SAlexander Motin mvs_detach(device_t dev)
177dd48af36SAlexander Motin {
178dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
179*3ddaf820SJohn Baldwin 	int error;
180dd48af36SAlexander Motin 
181dd48af36SAlexander Motin 	/* Detach & delete all children */
182*3ddaf820SJohn Baldwin 	error = bus_generic_detach(dev);
183*3ddaf820SJohn Baldwin 	if (error != 0)
184*3ddaf820SJohn Baldwin 		return (error);
18511bcf702SHans Petter Selasky 
186dd48af36SAlexander Motin 	/* Free interrupt. */
187dd48af36SAlexander Motin 	if (ctlr->irq.r_irq) {
188dd48af36SAlexander Motin 		bus_teardown_intr(dev, ctlr->irq.r_irq,
189dd48af36SAlexander Motin 		    ctlr->irq.handle);
190dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
191dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
192dd48af36SAlexander Motin 	}
193dd48af36SAlexander Motin 	pci_release_msi(dev);
194dd48af36SAlexander Motin 	/* Free memory. */
195dd48af36SAlexander Motin 	rman_fini(&ctlr->sc_iomem);
196dd48af36SAlexander Motin 	if (ctlr->r_mem)
197dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
198dd48af36SAlexander Motin 	mtx_destroy(&ctlr->mtx);
199dd48af36SAlexander Motin 	return (0);
200dd48af36SAlexander Motin }
201dd48af36SAlexander Motin 
202dd48af36SAlexander Motin static int
203dd48af36SAlexander Motin mvs_ctlr_setup(device_t dev)
204dd48af36SAlexander Motin {
205dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
206dd48af36SAlexander Motin 	int i, ccc = ctlr->ccc, cccc = ctlr->cccc, ccim = 0;
207dd48af36SAlexander Motin 
208dd48af36SAlexander Motin 	/* Mask chip interrupts */
209dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_MIM, 0x00000000);
210dd48af36SAlexander Motin 	/* Mask PCI interrupts */
211dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_PCIIM, 0x00000000);
212dd48af36SAlexander Motin 	/* Clear PCI interrupts */
213dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_PCIIC, 0x00000000);
214dd48af36SAlexander Motin 	if (ccc && bootverbose) {
215dd48af36SAlexander Motin 		device_printf(dev,
216dd48af36SAlexander Motin 		    "CCC with %dus/%dcmd enabled\n",
217dd48af36SAlexander Motin 		    ctlr->ccc, ctlr->cccc);
218dd48af36SAlexander Motin 	}
219dd48af36SAlexander Motin 	ccc *= 150;
220dd48af36SAlexander Motin 	/* Configure chip-global CCC */
221dd48af36SAlexander Motin 	if (ctlr->channels > 4 && (ctlr->quirks & MVS_Q_GENI) == 0) {
222dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_ICT, cccc);
223dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_ITT, ccc);
224dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_ICC, ~CHIP_ICC_ALL_PORTS);
225dd48af36SAlexander Motin 		if (ccc)
226dd48af36SAlexander Motin 			ccim |= IC_ALL_PORTS_COAL_DONE;
227dd48af36SAlexander Motin 		ccc = 0;
228dd48af36SAlexander Motin 		cccc = 0;
229dd48af36SAlexander Motin 	}
230dd48af36SAlexander Motin 	for (i = 0; i < ctlr->channels / 4; i++) {
231dd48af36SAlexander Motin 		/* Configure per-HC CCC */
232dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, HC_BASE(i) + HC_ICT, cccc);
233dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, HC_BASE(i) + HC_ITT, ccc);
234dd48af36SAlexander Motin 		if (ccc)
235dd48af36SAlexander Motin 			ccim |= (IC_HC0_COAL_DONE << (i * IC_HC_SHIFT));
236dd48af36SAlexander Motin 		/* Clear HC interrupts */
237dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, HC_BASE(i) + HC_IC, 0x00000000);
238dd48af36SAlexander Motin 	}
239dd48af36SAlexander Motin 	/* Enable chip interrupts */
240dd48af36SAlexander Motin 	ctlr->gmim = (ccim ? ccim : (IC_DONE_HC0 | IC_DONE_HC1)) |
241dd48af36SAlexander Motin 	     IC_ERR_HC0 | IC_ERR_HC1;
242dd48af36SAlexander Motin 	ctlr->mim = ctlr->gmim | ctlr->pmim;
243dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_MIM, ctlr->mim);
244dd48af36SAlexander Motin 	/* Enable PCI interrupts */
245dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_PCIIM, 0x007fffff);
246dd48af36SAlexander Motin 	return (0);
247dd48af36SAlexander Motin }
248dd48af36SAlexander Motin 
249dd48af36SAlexander Motin static void
250dd48af36SAlexander Motin mvs_edma(device_t dev, device_t child, int mode)
251dd48af36SAlexander Motin {
252dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
253dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
254dd48af36SAlexander Motin 	int bit = IC_DONE_IRQ << (unit * 2 + unit / 4) ;
255dd48af36SAlexander Motin 
256dd48af36SAlexander Motin 	if (ctlr->ccc == 0)
257dd48af36SAlexander Motin 		return;
258dd48af36SAlexander Motin 	/* CCC is not working for non-EDMA mode. Unmask device interrupts. */
259dd48af36SAlexander Motin 	mtx_lock(&ctlr->mtx);
260dd48af36SAlexander Motin 	if (mode == MVS_EDMA_OFF)
261dd48af36SAlexander Motin 		ctlr->pmim |= bit;
262dd48af36SAlexander Motin 	else
263dd48af36SAlexander Motin 		ctlr->pmim &= ~bit;
264dd48af36SAlexander Motin 	ctlr->mim = ctlr->gmim | ctlr->pmim;
265dd48af36SAlexander Motin 	if (!ctlr->msia)
266dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_MIM, ctlr->mim);
267dd48af36SAlexander Motin 	mtx_unlock(&ctlr->mtx);
268dd48af36SAlexander Motin }
269dd48af36SAlexander Motin 
270dd48af36SAlexander Motin static int
271dd48af36SAlexander Motin mvs_suspend(device_t dev)
272dd48af36SAlexander Motin {
273dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
274dd48af36SAlexander Motin 
275dd48af36SAlexander Motin 	bus_generic_suspend(dev);
276dd48af36SAlexander Motin 	/* Mask chip interrupts */
277dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_MIM, 0x00000000);
278dd48af36SAlexander Motin 	/* Mask PCI interrupts */
279dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_PCIIM, 0x00000000);
280dd48af36SAlexander Motin 	return 0;
281dd48af36SAlexander Motin }
282dd48af36SAlexander Motin 
283dd48af36SAlexander Motin static int
284dd48af36SAlexander Motin mvs_resume(device_t dev)
285dd48af36SAlexander Motin {
286dd48af36SAlexander Motin 
287dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
288dd48af36SAlexander Motin 	return (bus_generic_resume(dev));
289dd48af36SAlexander Motin }
290dd48af36SAlexander Motin 
291dd48af36SAlexander Motin static int
292dd48af36SAlexander Motin mvs_setup_interrupt(device_t dev)
293dd48af36SAlexander Motin {
294dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
295dd48af36SAlexander Motin 	int msi = 0;
296dd48af36SAlexander Motin 
297dd48af36SAlexander Motin 	/* Process hints. */
298dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
299dd48af36SAlexander Motin 	    device_get_unit(dev), "msi", &msi);
300dd48af36SAlexander Motin 	if (msi < 0)
301dd48af36SAlexander Motin 		msi = 0;
302dd48af36SAlexander Motin 	else if (msi > 0)
303dd48af36SAlexander Motin 		msi = min(1, pci_msi_count(dev));
304dd48af36SAlexander Motin 	/* Allocate MSI if needed/present. */
305dd48af36SAlexander Motin 	if (msi && pci_alloc_msi(dev, &msi) != 0)
306dd48af36SAlexander Motin 		msi = 0;
307dd48af36SAlexander Motin 	ctlr->msi = msi;
308dd48af36SAlexander Motin 	/* Allocate all IRQs. */
309dd48af36SAlexander Motin 	ctlr->irq.r_irq_rid = msi ? 1 : 0;
310dd48af36SAlexander Motin 	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
311dd48af36SAlexander Motin 	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
312dd48af36SAlexander Motin 		device_printf(dev, "unable to map interrupt\n");
313dd48af36SAlexander Motin 		return (ENXIO);
314dd48af36SAlexander Motin 	}
315dd48af36SAlexander Motin 	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
316dd48af36SAlexander Motin 	    mvs_intr, ctlr, &ctlr->irq.handle))) {
317dd48af36SAlexander Motin 		device_printf(dev, "unable to setup interrupt\n");
318dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
319dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
32087d8fcc8SPedro F. Giffuni 		ctlr->irq.r_irq = NULL;
321dd48af36SAlexander Motin 		return (ENXIO);
322dd48af36SAlexander Motin 	}
323dd48af36SAlexander Motin 	return (0);
324dd48af36SAlexander Motin }
325dd48af36SAlexander Motin 
326dd48af36SAlexander Motin /*
327dd48af36SAlexander Motin  * Common case interrupt handler.
328dd48af36SAlexander Motin  */
329dd48af36SAlexander Motin static void
330dd48af36SAlexander Motin mvs_intr(void *data)
331dd48af36SAlexander Motin {
332dd48af36SAlexander Motin 	struct mvs_controller *ctlr = data;
333dd48af36SAlexander Motin 	struct mvs_intr_arg arg;
334dd48af36SAlexander Motin 	void (*function)(void *);
335dd48af36SAlexander Motin 	int p;
336dd48af36SAlexander Motin 	u_int32_t ic, aic;
337dd48af36SAlexander Motin 
338dd48af36SAlexander Motin 	ic = ATA_INL(ctlr->r_mem, CHIP_MIC);
339dd48af36SAlexander Motin 	if (ctlr->msi) {
340caa7e52fSEitan Adler 		/* We have to mask MSI during processing. */
341dd48af36SAlexander Motin 		mtx_lock(&ctlr->mtx);
342dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_MIM, 0);
343dd48af36SAlexander Motin 		ctlr->msia = 1; /* Deny MIM update during processing. */
344dd48af36SAlexander Motin 		mtx_unlock(&ctlr->mtx);
345dd48af36SAlexander Motin 	} else if (ic == 0)
346dd48af36SAlexander Motin 		return;
347dd48af36SAlexander Motin 	/* Acknowledge all-ports CCC interrupt. */
348dd48af36SAlexander Motin 	if (ic & IC_ALL_PORTS_COAL_DONE)
349dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_ICC, ~CHIP_ICC_ALL_PORTS);
350dd48af36SAlexander Motin 	for (p = 0; p < ctlr->channels; p++) {
351dd48af36SAlexander Motin 		if ((p & 3) == 0) {
352dd48af36SAlexander Motin 			if (p != 0)
353dd48af36SAlexander Motin 				ic >>= 1;
354dd48af36SAlexander Motin 			if ((ic & IC_HC0) == 0) {
355dd48af36SAlexander Motin 				p += 3;
356dd48af36SAlexander Motin 				ic >>= 8;
357dd48af36SAlexander Motin 				continue;
358dd48af36SAlexander Motin 			}
359dd48af36SAlexander Motin 			/* Acknowledge interrupts of this HC. */
360dd48af36SAlexander Motin 			aic = 0;
361dd48af36SAlexander Motin 			if (ic & (IC_DONE_IRQ << 0))
362dd48af36SAlexander Motin 				aic |= HC_IC_DONE(0) | HC_IC_DEV(0);
363dd48af36SAlexander Motin 			if (ic & (IC_DONE_IRQ << 2))
364dd48af36SAlexander Motin 				aic |= HC_IC_DONE(1) | HC_IC_DEV(1);
365dd48af36SAlexander Motin 			if (ic & (IC_DONE_IRQ << 4))
366dd48af36SAlexander Motin 				aic |= HC_IC_DONE(2) | HC_IC_DEV(2);
367dd48af36SAlexander Motin 			if (ic & (IC_DONE_IRQ << 6))
368dd48af36SAlexander Motin 				aic |= HC_IC_DONE(3) | HC_IC_DEV(3);
369dd48af36SAlexander Motin 			if (ic & IC_HC0_COAL_DONE)
370dd48af36SAlexander Motin 				aic |= HC_IC_COAL;
371dd48af36SAlexander Motin 			ATA_OUTL(ctlr->r_mem, HC_BASE(p == 4) + HC_IC, ~aic);
372dd48af36SAlexander Motin 		}
373dd48af36SAlexander Motin 		/* Call per-port interrupt handler. */
374dd48af36SAlexander Motin 		arg.cause = ic & (IC_ERR_IRQ|IC_DONE_IRQ);
375dd48af36SAlexander Motin 		if ((arg.cause != 0) &&
376dd48af36SAlexander Motin 		    (function = ctlr->interrupt[p].function)) {
377dd48af36SAlexander Motin 			arg.arg = ctlr->interrupt[p].argument;
378dd48af36SAlexander Motin 			function(&arg);
379dd48af36SAlexander Motin 		}
380dd48af36SAlexander Motin 		ic >>= 2;
381dd48af36SAlexander Motin 	}
382dd48af36SAlexander Motin 	if (ctlr->msi) {
383dd48af36SAlexander Motin 		/* Unmasking MSI triggers next interrupt, if needed. */
384dd48af36SAlexander Motin 		mtx_lock(&ctlr->mtx);
385dd48af36SAlexander Motin 		ctlr->msia = 0;	/* Allow MIM update. */
386dd48af36SAlexander Motin 		ATA_OUTL(ctlr->r_mem, CHIP_MIM, ctlr->mim);
387dd48af36SAlexander Motin 		mtx_unlock(&ctlr->mtx);
388dd48af36SAlexander Motin 	}
389dd48af36SAlexander Motin }
390dd48af36SAlexander Motin 
391dd48af36SAlexander Motin static struct resource *
392dd48af36SAlexander Motin mvs_alloc_resource(device_t dev, device_t child, int type, int *rid,
3932dd1bdf1SJustin Hibbits 		       rman_res_t start, rman_res_t end, rman_res_t count,
3942dd1bdf1SJustin Hibbits 		       u_int flags)
395dd48af36SAlexander Motin {
396dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
397dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
398dd48af36SAlexander Motin 	struct resource *res = NULL;
399dd48af36SAlexander Motin 	int offset = HC_BASE(unit >> 2) + PORT_BASE(unit & 0x03);
400d8d7c6b1SJustin Hibbits 	rman_res_t st;
401dd48af36SAlexander Motin 
402dd48af36SAlexander Motin 	switch (type) {
403dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
404dd48af36SAlexander Motin 		st = rman_get_start(ctlr->r_mem);
405dd48af36SAlexander Motin 		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
406dd48af36SAlexander Motin 		    st + offset + PORT_SIZE - 1, PORT_SIZE, RF_ACTIVE, child);
407dd48af36SAlexander Motin 		if (res) {
408dd48af36SAlexander Motin 			bus_space_handle_t bsh;
409dd48af36SAlexander Motin 			bus_space_tag_t bst;
410dd48af36SAlexander Motin 			bsh = rman_get_bushandle(ctlr->r_mem);
411dd48af36SAlexander Motin 			bst = rman_get_bustag(ctlr->r_mem);
412dd48af36SAlexander Motin 			bus_space_subregion(bst, bsh, offset, PORT_SIZE, &bsh);
413dd48af36SAlexander Motin 			rman_set_bushandle(res, bsh);
414dd48af36SAlexander Motin 			rman_set_bustag(res, bst);
415dd48af36SAlexander Motin 		}
416dd48af36SAlexander Motin 		break;
417dd48af36SAlexander Motin 	case SYS_RES_IRQ:
418dd48af36SAlexander Motin 		if (*rid == ATA_IRQ_RID)
419dd48af36SAlexander Motin 			res = ctlr->irq.r_irq;
420dd48af36SAlexander Motin 		break;
421dd48af36SAlexander Motin 	}
422dd48af36SAlexander Motin 	return (res);
423dd48af36SAlexander Motin }
424dd48af36SAlexander Motin 
425dd48af36SAlexander Motin static int
4269dbf5b0eSJohn Baldwin mvs_release_resource(device_t dev, device_t child, struct resource *r)
427dd48af36SAlexander Motin {
428dd48af36SAlexander Motin 
4299dbf5b0eSJohn Baldwin 	switch (rman_get_type(r)) {
430dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
431dd48af36SAlexander Motin 		rman_release_resource(r);
432dd48af36SAlexander Motin 		return (0);
433dd48af36SAlexander Motin 	case SYS_RES_IRQ:
4349dbf5b0eSJohn Baldwin 		if (rman_get_rid(r) != ATA_IRQ_RID)
435dd48af36SAlexander Motin 			return ENOENT;
436dd48af36SAlexander Motin 		return (0);
437dd48af36SAlexander Motin 	}
438dd48af36SAlexander Motin 	return (EINVAL);
439dd48af36SAlexander Motin }
440dd48af36SAlexander Motin 
441dd48af36SAlexander Motin static int
442dd48af36SAlexander Motin mvs_setup_intr(device_t dev, device_t child, struct resource *irq,
443dd48af36SAlexander Motin 		   int flags, driver_filter_t *filter, driver_intr_t *function,
444dd48af36SAlexander Motin 		   void *argument, void **cookiep)
445dd48af36SAlexander Motin {
446dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
447dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
448dd48af36SAlexander Motin 
449dd48af36SAlexander Motin 	if (filter != NULL) {
450dd48af36SAlexander Motin 		printf("mvs.c: we cannot use a filter here\n");
451dd48af36SAlexander Motin 		return (EINVAL);
452dd48af36SAlexander Motin 	}
453dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = function;
454dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = argument;
455dd48af36SAlexander Motin 	return (0);
456dd48af36SAlexander Motin }
457dd48af36SAlexander Motin 
458dd48af36SAlexander Motin static int
459dd48af36SAlexander Motin mvs_teardown_intr(device_t dev, device_t child, struct resource *irq,
460dd48af36SAlexander Motin 		      void *cookie)
461dd48af36SAlexander Motin {
462dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
463dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
464dd48af36SAlexander Motin 
465dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = NULL;
466dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = NULL;
467dd48af36SAlexander Motin 	return (0);
468dd48af36SAlexander Motin }
469dd48af36SAlexander Motin 
470dd48af36SAlexander Motin static int
471dd48af36SAlexander Motin mvs_print_child(device_t dev, device_t child)
472dd48af36SAlexander Motin {
473dd48af36SAlexander Motin 	int retval;
474dd48af36SAlexander Motin 
475dd48af36SAlexander Motin 	retval = bus_print_child_header(dev, child);
476dd48af36SAlexander Motin 	retval += printf(" at channel %d",
477dd48af36SAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
478dd48af36SAlexander Motin 	retval += bus_print_child_footer(dev, child);
479dd48af36SAlexander Motin 
480dd48af36SAlexander Motin 	return (retval);
481dd48af36SAlexander Motin }
482dd48af36SAlexander Motin 
483445cc79cSAlexander Motin static int
484ddfc9c4cSWarner Losh mvs_child_location(device_t dev, device_t child, struct sbuf *sb)
485445cc79cSAlexander Motin {
486445cc79cSAlexander Motin 
487ddfc9c4cSWarner Losh 	sbuf_printf(sb, "channel=%d",
488445cc79cSAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
489445cc79cSAlexander Motin 	return (0);
490445cc79cSAlexander Motin }
491445cc79cSAlexander Motin 
492ca114192SAlexander Motin static bus_dma_tag_t
493ca114192SAlexander Motin mvs_get_dma_tag(device_t bus, device_t child)
494ca114192SAlexander Motin {
495ca114192SAlexander Motin 
496ca114192SAlexander Motin 	return (bus_get_dma_tag(bus));
497ca114192SAlexander Motin }
498ca114192SAlexander Motin 
499dd48af36SAlexander Motin static device_method_t mvs_methods[] = {
500dd48af36SAlexander Motin 	DEVMETHOD(device_probe,     mvs_probe),
501dd48af36SAlexander Motin 	DEVMETHOD(device_attach,    mvs_attach),
502dd48af36SAlexander Motin 	DEVMETHOD(device_detach,    mvs_detach),
503dd48af36SAlexander Motin 	DEVMETHOD(device_suspend,   mvs_suspend),
504dd48af36SAlexander Motin 	DEVMETHOD(device_resume,    mvs_resume),
505dd48af36SAlexander Motin 	DEVMETHOD(bus_print_child,  mvs_print_child),
506dd48af36SAlexander Motin 	DEVMETHOD(bus_alloc_resource,       mvs_alloc_resource),
507dd48af36SAlexander Motin 	DEVMETHOD(bus_release_resource,     mvs_release_resource),
508dd48af36SAlexander Motin 	DEVMETHOD(bus_setup_intr,   mvs_setup_intr),
509dd48af36SAlexander Motin 	DEVMETHOD(bus_teardown_intr,mvs_teardown_intr),
510ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_location, mvs_child_location),
511ca114192SAlexander Motin 	DEVMETHOD(bus_get_dma_tag,  mvs_get_dma_tag),
512dd48af36SAlexander Motin 	DEVMETHOD(mvs_edma,         mvs_edma),
513dd48af36SAlexander Motin 	{ 0, 0 }
514dd48af36SAlexander Motin };
515dd48af36SAlexander Motin static driver_t mvs_driver = {
516dd48af36SAlexander Motin         "mvs",
517dd48af36SAlexander Motin         mvs_methods,
518dd48af36SAlexander Motin         sizeof(struct mvs_controller)
519dd48af36SAlexander Motin };
520827252eeSJohn Baldwin DRIVER_MODULE(mvs, pci, mvs_driver, 0, 0);
5210dc34160SWarner Losh MODULE_PNP_INFO("W32:vendor/device", pci, mvs, mvs_ids,
5220dc34160SWarner Losh     nitems(mvs_ids) - 1);
523dd48af36SAlexander Motin MODULE_VERSION(mvs, 1);
524dd48af36SAlexander Motin MODULE_DEPEND(mvs, cam, 1, 1, 1);
525