1db57feb7SJonathan Lemon /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4ee7eb00eSJonathan Lemon * Copyright (c) 1999,2000 Jonathan Lemon
5db57feb7SJonathan Lemon * All rights reserved.
6db57feb7SJonathan Lemon *
7db57feb7SJonathan Lemon * Redistribution and use in source and binary forms, with or without
8db57feb7SJonathan Lemon * modification, are permitted provided that the following conditions
9db57feb7SJonathan Lemon * are met:
10db57feb7SJonathan Lemon * 1. Redistributions of source code must retain the above copyright
11db57feb7SJonathan Lemon * notice, this list of conditions and the following disclaimer.
12db57feb7SJonathan Lemon * 2. Redistributions in binary form must reproduce the above copyright
13db57feb7SJonathan Lemon * notice, this list of conditions and the following disclaimer in the
14db57feb7SJonathan Lemon * documentation and/or other materials provided with the distribution.
15db57feb7SJonathan Lemon *
16db57feb7SJonathan Lemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17db57feb7SJonathan Lemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18db57feb7SJonathan Lemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19db57feb7SJonathan Lemon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20db57feb7SJonathan Lemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21db57feb7SJonathan Lemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22db57feb7SJonathan Lemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23db57feb7SJonathan Lemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24db57feb7SJonathan Lemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25db57feb7SJonathan Lemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26db57feb7SJonathan Lemon * SUCH DAMAGE.
27db57feb7SJonathan Lemon */
28db57feb7SJonathan Lemon
29db57feb7SJonathan Lemon #include <sys/param.h>
30db57feb7SJonathan Lemon #include <sys/systm.h>
31db57feb7SJonathan Lemon #include <sys/kernel.h>
32fe12f24bSPoul-Henning Kamp #include <sys/module.h>
33db57feb7SJonathan Lemon
349626b608SPoul-Henning Kamp #include <sys/bio.h>
35db57feb7SJonathan Lemon #include <sys/bus.h>
36e5dc8339SPoul-Henning Kamp #include <sys/conf.h>
37e2e050c8SConrad Meyer #include <sys/lock.h>
38e2e050c8SConrad Meyer #include <sys/mutex.h>
39db57feb7SJonathan Lemon
40db57feb7SJonathan Lemon #include <machine/bus.h>
41db57feb7SJonathan Lemon #include <machine/resource.h>
42db57feb7SJonathan Lemon #include <sys/rman.h>
43db57feb7SJonathan Lemon
4477e6a3b2SWarner Losh #include <dev/pci/pcireg.h>
4577e6a3b2SWarner Losh #include <dev/pci/pcivar.h>
46db57feb7SJonathan Lemon
47891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
48891619a6SPoul-Henning Kamp
49db57feb7SJonathan Lemon #include <dev/ida/idavar.h>
50ee7eb00eSJonathan Lemon #include <dev/ida/idareg.h>
51db57feb7SJonathan Lemon
52db57feb7SJonathan Lemon #define IDA_PCI_MAX_DMA_ADDR 0xFFFFFFFF
53db57feb7SJonathan Lemon #define IDA_PCI_MAX_DMA_COUNT 0xFFFFFFFF
54db57feb7SJonathan Lemon
55e27951b2SJohn Baldwin #define IDA_PCI_MEMADDR PCIR_BAR(1) /* Mem I/O Address */
56db57feb7SJonathan Lemon
57db57feb7SJonathan Lemon #define IDA_DEVICEID_SMART 0xAE100E11
58f3cddbbdSJonathan Lemon #define IDA_DEVICEID_DEC_SMART 0x00461011
591ec5e8e6SJonathan Lemon #define IDA_DEVICEID_NCR_53C1510 0x00101000
60db57feb7SJonathan Lemon
61ee7eb00eSJonathan Lemon static int
ida_v3_fifo_full(struct ida_softc * ida)62ee7eb00eSJonathan Lemon ida_v3_fifo_full(struct ida_softc *ida)
63ee7eb00eSJonathan Lemon {
64ee7eb00eSJonathan Lemon return (ida_inl(ida, R_CMD_FIFO) == 0);
65ee7eb00eSJonathan Lemon }
66db57feb7SJonathan Lemon
67ee7eb00eSJonathan Lemon static void
ida_v3_submit(struct ida_softc * ida,struct ida_qcb * qcb)68ee7eb00eSJonathan Lemon ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
69ee7eb00eSJonathan Lemon {
70ee7eb00eSJonathan Lemon ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
71ee7eb00eSJonathan Lemon }
72ee7eb00eSJonathan Lemon
73ee7eb00eSJonathan Lemon static bus_addr_t
ida_v3_done(struct ida_softc * ida)74ee7eb00eSJonathan Lemon ida_v3_done(struct ida_softc *ida)
75ee7eb00eSJonathan Lemon {
76fc601c53SMatthew N. Dodd bus_addr_t completed;
77fc601c53SMatthew N. Dodd
78fc601c53SMatthew N. Dodd completed = ida_inl(ida, R_DONE_FIFO);
79fc601c53SMatthew N. Dodd if (completed == -1) {
80fc601c53SMatthew N. Dodd return (0); /* fifo is empty */
81fc601c53SMatthew N. Dodd }
82fc601c53SMatthew N. Dodd return (completed);
83ee7eb00eSJonathan Lemon }
84ee7eb00eSJonathan Lemon
85ee7eb00eSJonathan Lemon static int
ida_v3_int_pending(struct ida_softc * ida)86ee7eb00eSJonathan Lemon ida_v3_int_pending(struct ida_softc *ida)
87ee7eb00eSJonathan Lemon {
88ee7eb00eSJonathan Lemon return (ida_inl(ida, R_INT_PENDING));
89ee7eb00eSJonathan Lemon }
90ee7eb00eSJonathan Lemon
91ee7eb00eSJonathan Lemon static void
ida_v3_int_enable(struct ida_softc * ida,int enable)92ee7eb00eSJonathan Lemon ida_v3_int_enable(struct ida_softc *ida, int enable)
93ee7eb00eSJonathan Lemon {
9455d782fcSJonathan Lemon if (enable)
9555d782fcSJonathan Lemon ida->flags |= IDA_INTERRUPTS;
9655d782fcSJonathan Lemon else
9755d782fcSJonathan Lemon ida->flags &= ~IDA_INTERRUPTS;
98ee7eb00eSJonathan Lemon ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
99ee7eb00eSJonathan Lemon }
100ee7eb00eSJonathan Lemon
101ee7eb00eSJonathan Lemon static int
ida_v4_fifo_full(struct ida_softc * ida)102ee7eb00eSJonathan Lemon ida_v4_fifo_full(struct ida_softc *ida)
103ee7eb00eSJonathan Lemon {
104ee7eb00eSJonathan Lemon return (ida_inl(ida, R_42XX_REQUEST) != 0);
105ee7eb00eSJonathan Lemon }
106ee7eb00eSJonathan Lemon
107ee7eb00eSJonathan Lemon static void
ida_v4_submit(struct ida_softc * ida,struct ida_qcb * qcb)108ee7eb00eSJonathan Lemon ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
109ee7eb00eSJonathan Lemon {
110ee7eb00eSJonathan Lemon ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
111ee7eb00eSJonathan Lemon }
112ee7eb00eSJonathan Lemon
113ee7eb00eSJonathan Lemon static bus_addr_t
ida_v4_done(struct ida_softc * ida)114ee7eb00eSJonathan Lemon ida_v4_done(struct ida_softc *ida)
115ee7eb00eSJonathan Lemon {
116ee7eb00eSJonathan Lemon bus_addr_t completed;
117ee7eb00eSJonathan Lemon
118ee7eb00eSJonathan Lemon completed = ida_inl(ida, R_42XX_REPLY);
119ee7eb00eSJonathan Lemon if (completed == -1)
120ee7eb00eSJonathan Lemon return (0); /* fifo is empty */
121ee7eb00eSJonathan Lemon ida_outl(ida, R_42XX_REPLY, 0); /* confirm read */
122ee7eb00eSJonathan Lemon return (completed);
123ee7eb00eSJonathan Lemon }
124ee7eb00eSJonathan Lemon
125ee7eb00eSJonathan Lemon static int
ida_v4_int_pending(struct ida_softc * ida)126ee7eb00eSJonathan Lemon ida_v4_int_pending(struct ida_softc *ida)
127ee7eb00eSJonathan Lemon {
128ee7eb00eSJonathan Lemon return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
129ee7eb00eSJonathan Lemon }
130ee7eb00eSJonathan Lemon
131ee7eb00eSJonathan Lemon static void
ida_v4_int_enable(struct ida_softc * ida,int enable)132ee7eb00eSJonathan Lemon ida_v4_int_enable(struct ida_softc *ida, int enable)
133ee7eb00eSJonathan Lemon {
13455d782fcSJonathan Lemon if (enable)
13555d782fcSJonathan Lemon ida->flags |= IDA_INTERRUPTS;
13655d782fcSJonathan Lemon else
13755d782fcSJonathan Lemon ida->flags &= ~IDA_INTERRUPTS;
138ee7eb00eSJonathan Lemon ida_outl(ida, R_42XX_INT_MASK,
139ee7eb00eSJonathan Lemon enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
140ee7eb00eSJonathan Lemon }
141ee7eb00eSJonathan Lemon
142ee7eb00eSJonathan Lemon static struct ida_access ida_v3_access = {
143ee7eb00eSJonathan Lemon ida_v3_fifo_full,
144ee7eb00eSJonathan Lemon ida_v3_submit,
145ee7eb00eSJonathan Lemon ida_v3_done,
146ee7eb00eSJonathan Lemon ida_v3_int_pending,
147ee7eb00eSJonathan Lemon ida_v3_int_enable,
148ee7eb00eSJonathan Lemon };
149ee7eb00eSJonathan Lemon
150ee7eb00eSJonathan Lemon static struct ida_access ida_v4_access = {
151ee7eb00eSJonathan Lemon ida_v4_fifo_full,
152ee7eb00eSJonathan Lemon ida_v4_submit,
153ee7eb00eSJonathan Lemon ida_v4_done,
154ee7eb00eSJonathan Lemon ida_v4_int_pending,
155ee7eb00eSJonathan Lemon ida_v4_int_enable,
156ee7eb00eSJonathan Lemon };
157ee7eb00eSJonathan Lemon
158ee7eb00eSJonathan Lemon static struct ida_board board_id[] = {
1597c258c0eSJonathan Lemon { 0x40300E11, "Compaq SMART-2/P array controller",
1607c258c0eSJonathan Lemon &ida_v3_access, 0 },
1617c258c0eSJonathan Lemon { 0x40310E11, "Compaq SMART-2SL array controller",
1627c258c0eSJonathan Lemon &ida_v3_access, 0 },
1637c258c0eSJonathan Lemon { 0x40320E11, "Compaq Smart Array 3200 controller",
1647c258c0eSJonathan Lemon &ida_v3_access, 0 },
1657c258c0eSJonathan Lemon { 0x40330E11, "Compaq Smart Array 3100ES controller",
1667c258c0eSJonathan Lemon &ida_v3_access, 0 },
1677c258c0eSJonathan Lemon { 0x40340E11, "Compaq Smart Array 221 controller",
1687c258c0eSJonathan Lemon &ida_v3_access, 0 },
169ee7eb00eSJonathan Lemon
1707c258c0eSJonathan Lemon { 0x40400E11, "Compaq Integrated Array controller",
1717c258c0eSJonathan Lemon &ida_v4_access, IDA_FIRMWARE },
1727c258c0eSJonathan Lemon { 0x40480E11, "Compaq RAID LC2 controller",
1737c258c0eSJonathan Lemon &ida_v4_access, IDA_FIRMWARE },
1747c258c0eSJonathan Lemon { 0x40500E11, "Compaq Smart Array 4200 controller",
1757c258c0eSJonathan Lemon &ida_v4_access, 0 },
1767c258c0eSJonathan Lemon { 0x40510E11, "Compaq Smart Array 4250ES controller",
1777c258c0eSJonathan Lemon &ida_v4_access, 0 },
1787c258c0eSJonathan Lemon { 0x40580E11, "Compaq Smart Array 431 controller",
1797c258c0eSJonathan Lemon &ida_v4_access, 0 },
180f3cddbbdSJonathan Lemon
1817c258c0eSJonathan Lemon { 0, "", 0, 0 },
182db57feb7SJonathan Lemon };
183db57feb7SJonathan Lemon
184db57feb7SJonathan Lemon static int ida_pci_probe(device_t dev);
185db57feb7SJonathan Lemon static int ida_pci_attach(device_t dev);
186db57feb7SJonathan Lemon
187db57feb7SJonathan Lemon static device_method_t ida_pci_methods[] = {
188db57feb7SJonathan Lemon DEVMETHOD(device_probe, ida_pci_probe),
189db57feb7SJonathan Lemon DEVMETHOD(device_attach, ida_pci_attach),
190ee7eb00eSJonathan Lemon DEVMETHOD(device_detach, ida_detach),
191db57feb7SJonathan Lemon
1924b7ec270SMarius Strobl DEVMETHOD_END
193db57feb7SJonathan Lemon };
194db57feb7SJonathan Lemon
195db57feb7SJonathan Lemon static driver_t ida_pci_driver = {
196db57feb7SJonathan Lemon "ida",
197db57feb7SJonathan Lemon ida_pci_methods,
198db57feb7SJonathan Lemon sizeof(struct ida_softc)
199db57feb7SJonathan Lemon };
200db57feb7SJonathan Lemon
201ee7eb00eSJonathan Lemon static struct ida_board *
ida_pci_match(device_t dev)202834801e8SJonathan Lemon ida_pci_match(device_t dev)
203ee7eb00eSJonathan Lemon {
204ee7eb00eSJonathan Lemon int i;
205834801e8SJonathan Lemon u_int32_t id, sub_id;
206ee7eb00eSJonathan Lemon
207834801e8SJonathan Lemon id = pci_get_devid(dev);
208834801e8SJonathan Lemon sub_id = pci_get_subdevice(dev) << 16 | pci_get_subvendor(dev);
209834801e8SJonathan Lemon
210834801e8SJonathan Lemon if (id == IDA_DEVICEID_SMART ||
211834801e8SJonathan Lemon id == IDA_DEVICEID_DEC_SMART ||
212834801e8SJonathan Lemon id == IDA_DEVICEID_NCR_53C1510) {
213ee7eb00eSJonathan Lemon for (i = 0; board_id[i].board; i++)
214834801e8SJonathan Lemon if (board_id[i].board == sub_id)
215ee7eb00eSJonathan Lemon return (&board_id[i]);
216834801e8SJonathan Lemon }
217ee7eb00eSJonathan Lemon return (NULL);
218ee7eb00eSJonathan Lemon }
219ee7eb00eSJonathan Lemon
220db57feb7SJonathan Lemon static int
ida_pci_probe(device_t dev)221db57feb7SJonathan Lemon ida_pci_probe(device_t dev)
222db57feb7SJonathan Lemon {
223834801e8SJonathan Lemon struct ida_board *board = ida_pci_match(dev);
224db57feb7SJonathan Lemon
225ee7eb00eSJonathan Lemon if (board != NULL) {
226ee7eb00eSJonathan Lemon device_set_desc(dev, board->desc);
227b77e575eSWarner Losh return (BUS_PROBE_DEFAULT);
228db57feb7SJonathan Lemon }
229db57feb7SJonathan Lemon return (ENXIO);
230db57feb7SJonathan Lemon }
231db57feb7SJonathan Lemon
232db57feb7SJonathan Lemon static int
ida_pci_attach(device_t dev)233db57feb7SJonathan Lemon ida_pci_attach(device_t dev)
234db57feb7SJonathan Lemon {
235834801e8SJonathan Lemon struct ida_board *board = ida_pci_match(dev);
236280a27d2SJonathan Lemon u_int32_t id = pci_get_devid(dev);
237db57feb7SJonathan Lemon struct ida_softc *ida;
238db57feb7SJonathan Lemon int error, rid;
239db57feb7SJonathan Lemon
240db57feb7SJonathan Lemon ida = (struct ida_softc *)device_get_softc(dev);
241db57feb7SJonathan Lemon ida->dev = dev;
242ee7eb00eSJonathan Lemon ida->cmd = *board->accessor;
2437c258c0eSJonathan Lemon ida->flags = board->flags;
2446b5b57aeSJohn Baldwin mtx_init(&ida->lock, "ida", NULL, MTX_DEF);
2456b5b57aeSJohn Baldwin callout_init_mtx(&ida->ch, &ida->lock, 0);
246ee7eb00eSJonathan Lemon
247db57feb7SJonathan Lemon ida->regs_res_type = SYS_RES_MEMORY;
248db57feb7SJonathan Lemon ida->regs_res_id = IDA_PCI_MEMADDR;
249280a27d2SJonathan Lemon if (id == IDA_DEVICEID_DEC_SMART)
250e27951b2SJohn Baldwin ida->regs_res_id = PCIR_BAR(0);
2511277c3baSJonathan Lemon
2525f96beb9SNate Lawson ida->regs = bus_alloc_resource_any(dev, ida->regs_res_type,
2535f96beb9SNate Lawson &ida->regs_res_id, RF_ACTIVE);
254db57feb7SJonathan Lemon if (ida->regs == NULL) {
2551277c3baSJonathan Lemon device_printf(dev, "can't allocate memory resources\n");
256db57feb7SJonathan Lemon return (ENOMEM);
257db57feb7SJonathan Lemon }
258db57feb7SJonathan Lemon
259b75ab906SMatthew N. Dodd error = bus_dma_tag_create(
260b6f97155SScott Long /* parent */ bus_get_dma_tag(dev),
261b75ab906SMatthew N. Dodd /* alignment */ 1,
262b75ab906SMatthew N. Dodd /* boundary */ 0,
263b75ab906SMatthew N. Dodd /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
264b75ab906SMatthew N. Dodd /* highaddr */ BUS_SPACE_MAXADDR,
265b75ab906SMatthew N. Dodd /* filter */ NULL,
266b75ab906SMatthew N. Dodd /* filterarg */ NULL,
2676f954fb3SAlexander Motin /* maxsize */ BUS_SPACE_MAXSIZE_32BIT,
2686f954fb3SAlexander Motin /* nsegments */ BUS_SPACE_UNRESTRICTED,
269b75ab906SMatthew N. Dodd /* maxsegsize */ BUS_SPACE_MAXSIZE_32BIT,
270b75ab906SMatthew N. Dodd /* flags */ BUS_DMA_ALLOCNOW,
271b75ab906SMatthew N. Dodd /* lockfunc */ NULL,
272b75ab906SMatthew N. Dodd /* lockarg */ NULL,
273b75ab906SMatthew N. Dodd &ida->parent_dmat);
274db57feb7SJonathan Lemon if (error != 0) {
275db57feb7SJonathan Lemon device_printf(dev, "can't allocate DMA tag\n");
276db57feb7SJonathan Lemon ida_free(ida);
277db57feb7SJonathan Lemon return (ENOMEM);
278db57feb7SJonathan Lemon }
279db57feb7SJonathan Lemon
280db57feb7SJonathan Lemon rid = 0;
281db57feb7SJonathan Lemon ida->irq_res_type = SYS_RES_IRQ;
2825f96beb9SNate Lawson ida->irq = bus_alloc_resource_any(dev, ida->irq_res_type, &rid,
2835f96beb9SNate Lawson RF_ACTIVE | RF_SHAREABLE);
284db57feb7SJonathan Lemon if (ida->irq == NULL) {
285db57feb7SJonathan Lemon ida_free(ida);
286db57feb7SJonathan Lemon return (ENOMEM);
287db57feb7SJonathan Lemon }
2886b5b57aeSJohn Baldwin error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE,
289ef544f63SPaolo Pisati NULL, ida_intr, ida, &ida->ih);
290db57feb7SJonathan Lemon if (error) {
291db57feb7SJonathan Lemon device_printf(dev, "can't setup interrupt\n");
292db57feb7SJonathan Lemon ida_free(ida);
293db57feb7SJonathan Lemon return (ENOMEM);
294db57feb7SJonathan Lemon }
295db57feb7SJonathan Lemon
296a7f3dd6fSHans Petter Selasky error = ida_setup(ida);
297db57feb7SJonathan Lemon if (error) {
298db57feb7SJonathan Lemon ida_free(ida);
299db57feb7SJonathan Lemon return (error);
300db57feb7SJonathan Lemon }
301db57feb7SJonathan Lemon
302db57feb7SJonathan Lemon return (0);
303db57feb7SJonathan Lemon }
304db57feb7SJonathan Lemon
305fb9f63e8SJohn Baldwin DRIVER_MODULE(ida, pci, ida_pci_driver, 0, 0);
3060dc34160SWarner Losh MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ida, board_id,
3070dc34160SWarner Losh nitems(board_id) - 1);
308