Merge branch 'vendor/GMP'
[dragonfly.git] / sys / dev / raid / ida / ida_pci.c
1 /*-
2  * Copyright (c) 1999,2000 Jonathan Lemon
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ida/ida_pci.c,v 1.7.2.7 2001/07/30 20:29:58 jlemon Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
33
34 #include <sys/buf.h>
35 #include <sys/bus.h>
36 #include <sys/devicestat.h>
37 #include <sys/disk.h>
38 #include <sys/rman.h>
39
40 #include <bus/pci/pcireg.h>
41 #include <bus/pci/pcivar.h>
42
43 #include "idavar.h"
44 #include "idareg.h"
45
46 #define IDA_PCI_MAX_DMA_ADDR    0xFFFFFFFF
47 #define IDA_PCI_MAX_DMA_COUNT   0xFFFFFFFF
48
49 #define IDA_PCI_MEMADDR         (PCIR_MAPS + 4)         /* Mem I/O Address */
50
51 #define IDA_DEVICEID_SMART              0xAE100E11
52 #define IDA_DEVICEID_DEC_SMART          0x00461011
53 #define IDA_DEVICEID_NCR_53C1510        0x00101000
54
55 static int
56 ida_v3_fifo_full(struct ida_softc *ida)
57 {
58         return (ida_inl(ida, R_CMD_FIFO) == 0);
59 }
60
61 static void
62 ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
63 {
64         ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
65 }
66
67 static bus_addr_t
68 ida_v3_done(struct ida_softc *ida)
69 {
70         return (ida_inl(ida, R_DONE_FIFO));
71 }
72
73 static int
74 ida_v3_int_pending(struct ida_softc *ida)
75 {
76         return (ida_inl(ida, R_INT_PENDING));
77 }
78
79 static void
80 ida_v3_int_enable(struct ida_softc *ida, int enable)
81 {
82         if (enable)
83                 ida->flags |= IDA_INTERRUPTS;
84         else
85                 ida->flags &= ~IDA_INTERRUPTS;
86         ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
87 }
88
89 static int
90 ida_v4_fifo_full(struct ida_softc *ida)
91 {
92         return (ida_inl(ida, R_42XX_REQUEST) != 0);
93 }
94
95 static void
96 ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
97 {
98         ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
99 }
100
101 static bus_addr_t
102 ida_v4_done(struct ida_softc *ida)
103 {
104         bus_addr_t completed;
105
106         completed = ida_inl(ida, R_42XX_REPLY);
107         if (completed == -1)
108                 return (0);                     /* fifo is empty */
109         ida_outl(ida, R_42XX_REPLY, 0);         /* confirm read */
110         return (completed);
111 }
112
113 static int
114 ida_v4_int_pending(struct ida_softc *ida)
115 {
116         return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
117 }
118
119 static void
120 ida_v4_int_enable(struct ida_softc *ida, int enable)
121 {
122         if (enable)
123                 ida->flags |= IDA_INTERRUPTS;
124         else
125                 ida->flags &= ~IDA_INTERRUPTS;
126         ida_outl(ida, R_42XX_INT_MASK,
127             enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
128 }
129
130 static struct ida_access ida_v3_access = {
131         ida_v3_fifo_full,
132         ida_v3_submit,
133         ida_v3_done,
134         ida_v3_int_pending,
135         ida_v3_int_enable,
136 };
137
138 static struct ida_access ida_v4_access = {
139         ida_v4_fifo_full,
140         ida_v4_submit,
141         ida_v4_done,
142         ida_v4_int_pending,
143         ida_v4_int_enable,
144 };
145
146 static struct ida_board board_id[] = {
147         { 0x40300E11, "Compaq SMART-2/P array controller",
148             &ida_v3_access, 0 },
149         { 0x40310E11, "Compaq SMART-2SL array controller",
150             &ida_v3_access, 0 },
151         { 0x40320E11, "Compaq Smart Array 3200 controller",
152             &ida_v3_access, 0 },
153         { 0x40330E11, "Compaq Smart Array 3100ES controller",
154             &ida_v3_access, 0 },
155         { 0x40340E11, "Compaq Smart Array 221 controller",
156             &ida_v3_access, 0 },
157
158         { 0x40400E11, "Compaq Integrated Array controller",
159             &ida_v4_access, IDA_FIRMWARE },
160         { 0x40480E11, "Compaq RAID LC2 controller",
161             &ida_v4_access, IDA_FIRMWARE },
162         { 0x40500E11, "Compaq Smart Array 4200 controller",
163             &ida_v4_access, 0 },
164         { 0x40510E11, "Compaq Smart Array 4250ES controller",
165             &ida_v4_access, 0 },
166         { 0x40580E11, "Compaq Smart Array 431 controller",
167             &ida_v4_access, 0 },
168
169         { 0, "", 0, 0 },
170 };
171
172 static int ida_pci_probe(device_t dev);
173 static int ida_pci_attach(device_t dev);
174
175 static device_method_t ida_pci_methods[] = {
176         DEVMETHOD(device_probe,         ida_pci_probe),
177         DEVMETHOD(device_attach,        ida_pci_attach),
178         DEVMETHOD(device_detach,        ida_detach),
179
180         DEVMETHOD(bus_print_child,      bus_generic_print_child),
181
182         { 0, 0 }
183 };
184
185 static driver_t ida_pci_driver = {
186         "ida",
187         ida_pci_methods,
188         sizeof(struct ida_softc)
189 };
190
191 static devclass_t ida_devclass;
192
193 static struct ida_board *
194 ida_pci_match(device_t dev)
195 {
196         int i;
197         u_int32_t id, sub_id;
198
199         id = pci_get_devid(dev);
200         sub_id = pci_get_subdevice(dev) << 16 | pci_get_subvendor(dev);
201
202         if (id == IDA_DEVICEID_SMART ||
203             id == IDA_DEVICEID_DEC_SMART ||
204             id == IDA_DEVICEID_NCR_53C1510) {
205                 for (i = 0; board_id[i].board; i++)
206                         if (board_id[i].board == sub_id)
207                                 return (&board_id[i]);
208         }
209         return (NULL);
210 }
211
212 static int
213 ida_pci_probe(device_t dev)
214 {
215         struct ida_board *board = ida_pci_match(dev);
216
217         if (board != NULL) {
218                 device_set_desc(dev, board->desc);
219                 return (0);
220         }
221         return (ENXIO);
222 }
223
224 static int
225 ida_pci_attach(device_t dev)
226 {
227         struct ida_board *board = ida_pci_match(dev);
228         u_int32_t id = pci_get_devid(dev);
229         struct ida_softc *ida;
230         u_int command;
231         int error, rid;
232
233         command = pci_read_config(dev, PCIR_COMMAND, 1);
234
235         /*
236          * it appears that this board only does MEMIO access.
237          */
238         if ((command & PCIM_CMD_MEMEN) == 0) {
239                 device_printf(dev, "Only memory mapped I/O is supported\n");
240                 return (ENXIO);
241         }
242
243         ida = (struct ida_softc *)device_get_softc(dev);
244         ida->dev = dev;
245         ida->cmd = *board->accessor;
246         ida->flags = board->flags;
247
248         ida->regs_res_type = SYS_RES_MEMORY;
249         ida->regs_res_id = IDA_PCI_MEMADDR;
250         if (id == IDA_DEVICEID_DEC_SMART)
251                 ida->regs_res_id = PCIR_MAPS;
252
253         ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
254             &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
255         if (ida->regs == NULL) {
256                 device_printf(dev, "can't allocate memory resources\n");
257                 return (ENOMEM);
258         }
259
260         error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
261             /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
262             /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
263             /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
264             /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
265             &ida->parent_dmat);
266         if (error != 0) {
267                 device_printf(dev, "can't allocate DMA tag\n");
268                 ida_free(ida);
269                 return (ENOMEM);
270         }
271
272         rid = 0;
273         ida->irq_res_type = SYS_RES_IRQ;
274         ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
275             0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
276         if (ida->irq == NULL) {
277                 ida_free(ida);
278                 return (ENOMEM);
279         }
280         error = bus_setup_intr(dev, ida->irq, 0,
281                                ida_intr, ida, &ida->ih, NULL);
282         if (error) {
283                 device_printf(dev, "can't setup interrupt\n");
284                 ida_free(ida);
285                 return (ENOMEM);
286         }
287
288         error = ida_init(ida);
289         if (error) {
290                 ida_free(ida);
291                 return (error);
292         }
293         ida_attach(ida);
294         ida->flags |= IDA_ATTACHED;
295
296         return (0);
297 }
298
299 DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, NULL, NULL);